«

»

Feb 06 2008

Learning Flickr APIs: Part I

At my work, we are going to be starting a complete redesign of one of our sites in the next few weeks and we are preparing for it now. Just as I was leaving from work, my boss told me that he wants me to start using Flickr API’s for our photo galleries. I have had a pro flickr account for over a year and use it every day, but I have never wrapped my head around the API. (I don’t know why, it’s very easy)

If you want to use PHP to get the data, you would use the Serialized PHP Response Format in which you pass it information and it returns a multi-dimensional array. After looking at the PHP code, I realized that this would need to be a function since it is repeating a lot of information except a few spots. There are flickr libraries that you can use, but this is my first stab at it and I tried, but there were a few glitches. Here is a quick function I created to accomplish the request.

$api_key = 'some api key';
$user_id = 'some user id';

function make_call($params){
	global $api_key, $user_id; 

	$auth = array('api_key'=>$api_key,'format'=>'php_serial');
	$first_array = array_splice($params, 0, 0);
	$params = array_merge($first_array, $auth, $params);

	$encoded_params = array();
	foreach ($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
	}
	$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
	$ch = curl_init();
	$timeout = 5; // set to zero for no timeout
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $rsp = curl_exe(c)($ch); (NO PARENTHESIS AROUND THE (c) in exec....I could not save it without them)
	curl_close($ch);
	$rsp_obj = unserialize($rsp);

	return $rsp_obj;
}

This should be saved in a functions file and called from another page that included this page. Below is the code used to call the request. You would change the $params array to what is required in that particular method.

include("functions.php"); // to get function make_call

$params = array(
	'user_id'	=> $user_id,
	'photoset_id' => 'some photoset id',
	'method'	=> 'flickr.photosets.getPhotos'
);
$data = make_call($params);

Using the $data array, you can manipulate or retrieve the data how it fits your needs. This was thrown together quick to get something from flickr which I did. I displayed all the photos from one of my sets.

$data = make_call($params);
if($data['stat'] == 'ok'){
	$set_info = $data['photoset'];
	foreach($set_info['photo'] as $photos){
		$params = array(
			'user_id'	=> $user_id,
			'photo_id' => $photos['id'],
			'method'	=> 'flickr.photos.getInfo'
		);
		$data = make_call($params);
		$photo = $data['photo'];
		$farm_server =  'http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'];
		echo '';
	}
}

There may be a better way to accomplish this that I am not aware of, I just started looking at the API’s tonight. Hope this helps a basic request call.

About the author

Timothy Haroutunian

Timothy Haroutunian is a ServiceNow Cloud Implementation Specialist at Acorio. ServiceNow is an IT Management solution that allows for a complete view of your IT and physical environment.

Permanent link to this article: http://www.armenianeagle.com/2008/02/06/learning-flickr-apis-part-i/

http://pagead2.googlesyndication.com/pagead/show_ads.js

1 comment

1 ping

  1. Niels

    Thanks for writing this post! It’s very helpful!

  1. Armenian Eagle » Learning Flickr APIs: Part II - Uploading Files

    […] my first article about Flickr APIs, I needed to upload photos to flickr which was a lot harder than I thought it would be. I did some […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.