May 30 2010

Parsing Flickr Feed with PHP tutorial

Category: Php,Spiders & webbotsGiulio Pons @ 10:48 pm

I’ve spent about 30 minutes to find a javascript embed to print out a custom thumbs list of flickr photos, but I didn’t find anything clean enaugh… and I’ve not enaugh time to spend to read the flickr’s API’s…
So, I’ve searched for the feed of the user, and I’ve found it at the bottom of flickr’s pages: I’ve decided to grab the feed and parse it to get my custom gallery.
If you’re looking for the flickr’s feed it’s here, at the bottom:

flickr feed

If you click on the feed link you will open the feed (this can be shown in different ways depending on your browser). If you look the URL of the feed you’ve clicked, you can see that it contains the “id” of the feed on flickr database, here is the id:

feed id

Take the id. And then use this php function to grab and create the thumbs list, this code will simply output anchors and images, so you have to use css to customize it as you want:

function attr($s,$attrname) { // return html attribute
	preg_match_all('#\s*('.$attrname.')\s*=\s*["|\']([^"\']*)["|\']\s*#i', $s, $x);
	if (count($x)>=3) return $x[2][0]; else return "";
}

// id = id of the feed
// n = number of thumbs
function parseFlickrFeed($id,$n) {
	$url = "http://api.flickr.com/services/feeds/photos_public.gne?id={$id}&lang=it-it&format=rss_200";
	$s = file_get_contents($url);
	preg_match_all('#<item>(.*)</item>#Us', $s, $items);
	$out = "";
	for($i=0;$i<count($items[1]);$i++) {
		if($i>=$n) return $out;
		$item = $items[1][$i];
		preg_match_all('#<link>(.*)</link>#Us', $item, $temp);
		$link = $temp[1][0];
		preg_match_all('#<title>(.*)</title>#Us', $item, $temp);
		$title = $temp[1][0];
		preg_match_all('#<media:thumbnail([^>]*)>#Us', $item, $temp);
		$thumb = attr($temp[0][0],"url");
		$out.="<a href='$link' target='_blank' title=\"".str_replace('"','',$title)."\"><img src='$thumb'/></a>";
	}
	return $out;
}

// usage example:
echo parseFlickrFeed("16664181@N00",9);
// you have to use css to customize it

Like here:
flickr thumbs css

This code will be addedd to the next version of Mini Bots Class.

  • Share/Bookmark

Tags: , , , , , , ,


May 02 2010

10 htaccess usefull tips

Category: UncategorizedGiulio Pons @ 1:45 pm

The “.htaccess” is a configuration file that works at directory level on the web servers (on Apache, not on Microsoft IIS). The “ht” letters of “htaccess” stand for “hypertext”. Like the “ht” letters of the “http” protocol.

With the htaccess file you can do many usefull things, such as redirecting users to different pages and modify the urls. When you do these things you are “rewriting” the urls and you are using a module (a program) of the web server which is called “mod_rewrite”.

You can also set the error pages, specify passwords and much more. Here are a list of 10 usefull things I do with htaccess file on my works:

1) This script redirects all the calls to mp3 files to a php file, a mini proxy, that can do something usefull as tracking stats on a db:

# the +FollowSymlinks should be already setted, but if it isnt' this
# setting will turn it on. FollowSymlinks must be on to use the rewrite engine.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.mp3$ /proxy.php?z=$1.mp3 [QSA]

2) Suppose you want to automatically add www (if there isn’t) at the beginning of the url:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

3) If you don’t like the wasting of the 4 bytes of the “www.” string, you can use this htaccess configuration to sistematically remove the www from any url:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

4) This is a simple way to make urls readable: simply redefine them:

# from http://www.domain.com/article/18/readable-string
# to http://www.domain.com/article.php?id=18
# the [L] letter at the end means that if this rule match than it's the last and no more rules must be applied
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^article/([0-9]*)/(.*)$ /article.php?x=$1 [L]

5) redirect everything that isn’t a call to an existing page to a file and pass the querystring as a parameter. So you can do something like changing this http://www.domain.com/bradpitt to http://www.domain.com/proxy.php?user=bradpitt

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /proxy.php?user=$1 [QSA]

6) Make a basic authentication inside a directory by putting this .htaccess file in that directory.

# basic authentication
AuthType Basic
AuthName "Protected Area"
AuthUserFile /path/to/.htpasswd
Require valid-user

In the .htpasswd file you have to specify user and password, the password must be md5 encoded, like this for user “jack”:

jack:$apr1$ZVsMp...$I4UkebuYwg5QQ0cs7s921/

7) Define custom error documents, this do not require mod_rewrite.

ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

8 ) simple redirect to map old links to new ones when you re-style everything. This will help you not loosing traffic from google.

Redirect 301 /oldfile.html http://www.domain.com/newfile.html

9) When you have a big update to your site you can redirect every user to a “work in progress” page, than – when you finish – remember to remove this blocking htaccess file!

order deny,allow
deny from all
allow from 81.81.81.81
ErrorDocument 403 /wip.htm
<Files wip.htm>
allow from all
</Files>

Put your IP address where there is “81.81.81.81″, and put your wip.htm page.

10) prevent directory browsing by placing this line in your htaccess file:

Options All -Indexes
  • Share/Bookmark

Tags: , , , , , ,


Apr 27 2010

Correct headers to download a CSV from PHP

Category: PhpGiulio Pons @ 11:40 am

With these headers you can force a download from php, for example, to let the user download a csv frmatted text.
To make all the thing works you have to print data with “tabs” char, even if csv means “comma separeted value”. This is necessary because Microsoft Excel will understand tabs, but not commas.

header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"yourfilename.xls\"");
/*
      here print rows with values separeted by tab char: "\t"
*/
  • Share/Bookmark

Tags: , , , , ,


« Previous PageNext Page »