Learning to Love the API: Photo Examples

Wednesday, October 6th, 2010

Displaying Photos

Displaying photos is easy if you use a plugin to simplify the Flickr Services API. This example uses a local copy of jQuery Flickr by Daniel MacDonald. Here is what the example looks like live. You will need your own API key.

 

Even Better

The following is a function that I set up to display images galleries on metrofiets.com. The owners just build new sets and then enter the gallery type and set id via a custom WordPress panel (not shown).


 *  created: 	2010-01-14 
 *  updated: 	
 * ================================================================== */
 
/* === Embed Flickr (Where set by Custom Field Above) === */
// Now without PHPFlickr overhead thanks to http://blog.darkcrimson.com/2009/11/mastering-the-flickr-api-with-php-and-curl/

function embed_flickr ($type="", $setid="", $setowner=""){  // set owner could be used to link to image on flickr
	if(!$type || !$setid) {return false;}

	// Set up API call	  
	$params = array(
		'api_key'	=> '00000000000000000000000',
		'method'	=> 'flickr.photosets.getPhotos',
		'photoset_id'	=> $setid,
		'extras'	=> 'original_format',
		'format'	=> 'php_serial',
	);
	$encoded_params = array();
	foreach ($params as $k => $v){ $encoded_params[] = urlencode($k).'='.urlencode($v); }
	
	
	//Make the call via cURL
	$ch = curl_init();
	$timeout = 5; // set to zero for no timeout
	curl_setopt ($ch, CURLOPT_URL, 'http://api.flickr.com/services/rest/?'.implode('&', $encoded_params));
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$file_contents = curl_exec($ch);
	curl_close($ch);
	
	$rsp_obj = unserialize($file_contents);
	 
	 
	// built slideshow (type==1)
	if ($rsp_obj['stat']=='ok' && $type==1) {
	
		$photos = $rsp_obj["photoset"]["photo"];

		$outSmall	= "";
		$outNormal 	= "";
		
		foreach($photos as $photo) {	
			$farm              = $photo['farm'];
			$server            = $photo['server'];
			$photo_id          = $photo['id'];
			$secret            = $photo['secret'];
			$photo_title       = $photo['secret'];
			
			// unique id for each - id must start with alpha
			$outSmall = $outSmall.''.$photo['title'].''; 
			if($outNormal == ""){ // only preload first
			   $outNormal=''.$photo['title'].'';
			}// echo '
  • '; echo ' '; echo '
    '.$outNormal.'
    '; echo '
  • '; } // built gallery (type==2) else if ($rsp_obj['stat']=='ok' && $type==2) { $photos = $rsp_obj["photoset"]["photo"]; $outSmall = ""; $outNormal = ""; foreach($photos as $photo) { $farm = $photo['farm']; $server = $photo['server']; $photo_id = $photo['id']; $secret = $photo['secret']; $photo_title = $photo['secret']; // unique id for each - id must start with alpha $outSmall = $outSmall.''.$photo['title'].''; if($outNormal == ""){ // only preload first $outNormal=''.$photo['title'].''; }// echo '
  • '; echo '
    '.$outSmall.'
    '; echo '
    '.$outNormal.'
    '; echo ' '; echo '
  • '; } else { echo "Error building Flickr set. Error information - Set:'$setid' Type:'$type'"; } } ?>

    2 Responses to “Learning to Love the API: Photo Examples”


    1. gabriel nagmay (dot com) | Archive » Learning to Love the API Says:

      […] Photos (Flickr) […]


    2. gabriel nagmay (dot com) | Archive » ALL THE THINGS! Says:

      […] Photos API (Flickr) […]

    Leave a Reply

    You know you want to...