<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dark Crimson Blog &#187; CURL</title>
	<atom:link href="http://blog.darkcrimson.com/tag/curl/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.darkcrimson.com</link>
	<description>The Blog of Ben Lister</description>
	<lastBuildDate>Fri, 13 Aug 2010 02:18:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mastering the Flickr API with PHP and cURL</title>
		<link>http://blog.darkcrimson.com/2009/11/mastering-the-flickr-api-with-php-and-curl/</link>
		<comments>http://blog.darkcrimson.com/2009/11/mastering-the-flickr-api-with-php-and-curl/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 04:48:55 +0000</pubDate>
		<dc:creator>Ben Lister</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[CURL]]></category>
		<category><![CDATA[Flickr]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://blog.darkcrimson.com/?p=34</guid>
		<description><![CDATA[The Flickr API is a very powerful and easy to use way to put photos on your website without having to worry about bandwidth or server performance issues. This tutorial will cover how to make an API call for a photoset and then format and display a list of ~100px thumbnail images in unordered list.
Getting [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.flickr.com/services/api/" class="ext">Flickr API</a> is a very powerful and easy to use way to put photos on your website without having to worry about bandwidth or server performance issues. This tutorial will cover how to make an API call for a photoset and then format and display a list of ~100px thumbnail images in unordered list.<span id="more-34"></span></p>
<h3 class="blog_ttl">Getting An API Key</h3>
<p>To get started with the Flickr API, you need to sign into Flickr and <a href="http://www.flickr.com/services/apps/create/apply/" class="ext">Apply for an API key</a>. After you register for the API Key, you should browse through <a href="http://www.flickr.com/services/api/" target="_self">Flickr&#8217;s API page</a> to see some examples of what is possible. As you will see, there are quite a few methods listed which might seem a bit daunting. The nice thing is all of the provided methods are well documented and after one successful implementation, working through the rest is fairly repetitious.</p>
<h3 class="blog_ttl">Hot, Steamy PHP Action</h3>
<p>We will be using <a href="http://php.net/manual/en/book.curl.php" class="ext"><code>cURL</code></a> to get the data from Flickr. Some might prefer another method such as PHP&#8217;s <a href="http://php.net/manual/en/function.file-get-contents.php" class="ext"><code>file_get_contents</code></a>, there are advantages to both but you can read a book (or <a href="http://www.google.com/search?q=curl+vs+file_get_contents" class="ext">Google</a>) for more information on this.</p>
<p>Getting started with the cURL request:</p>
<pre name="code" class="php">$params = array(
	'api_key'	=&gt; 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
	'method'	=&gt; 'flickr.photosets.getPhotos',
	'photoset_id'	=&gt; '72157622566216264',
	'extras'	=> 'original_format',
	'format'	=&gt; 'php_serial'
);
$encoded_params = array();
foreach ($params as $k => $v){ $encoded_params[] = urlencode($k).'='.urlencode($v); }
</pre>
<p>In the code above we first put the API key and then choose the Method (which is listed on the Flickr API page link listed earlier in the tutorial). Next we specify the photo set id which for this example is the set seen in <a href="http://darkcrimson.com/portfolio" class="ext">my Portfolio</a>. The parameters for &#8220;Extras&#8221; depend on the Method used and desired options and range from date uploaded to author to tags. For this example we will be calling &#8220;original_format&#8221; which will pull a link to the full size version of the image. Finally, we choose &#8220;php_serial&#8221; as the format which will return a <a href="http://php.net/manual/en/function.serialize.php" class="ext">serialized</a> PHP string with the data (Alternatively you could use JSON but we can save that fun for a JavaScript tutorial).</p>
<p><em>Please note: </em>to use the &#8220;original_format&#8221; parameter you will need to be a <a href="http://www.flickr.com/upgrade/" class="ext">Flickr Pro</a> member.</p>
<pre name="code" class="php">$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://api.flickr.com/services/rest/?'.implode('&amp;', $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);</pre>
<p>The code above makes the cURL call to Flickr&#8217;s API with the data we set in the previous code block, <code>unserializes</code> the <code>cURL</code> object&#8217;s returned data and creates a new Array called <code>$rsp_obj.</code></p>
<p>Let&#8217;s pause and take a look at some of the data that was returned with a little <code>print_r($_rsp_ojb)</code> action:</p>
<pre name="code" class="php">Array
(
    [photoset] =&gt; Array
        (
            [id] =&gt; 72157622566216264
            [primary] =&gt; 4003748546
            [owner] =&gt; 7332638@N08
            [ownername] =&gt; benlister
            [photo] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [id] =&gt; 4003748546
                            [secret] =&gt; 69b188c174
                            [server] =&gt; 2643
                            [farm] =&gt; 3
                            [title] =&gt; Chicago (Montrose Harbor)
                            [isprimary] =&gt; 1
                            [originalsecret] =&gt; 728e01b6c1
                            [originalformat] =&gt; jpg
                        )

                    [1] =&gt; Array
                        (
                            [id] =&gt; 4052220478
                            [secret] =&gt; ec5e79cbbe
                            [server] =&gt; 2780
                            [farm] =&gt; 3
                            [title] =&gt; Seattle Sci-Fi Museum Exterior
                            [isprimary] =&gt; 0
                            [originalsecret] =&gt; 625519fc32
                            [originalformat] =&gt; jpg
                        )

..............</pre>
<p>The array output above tells us pretty  everything we will need to create links to all sizes of each photo in the set. To display each photo in the photo set we first need to step into the <code>[photo]</code> section of  the <code>[photoset]</code> array.</p>
<pre name="code" class="php">if ($rsp_obj['stat'] == 'ok') {

		$photos = $rsp_obj["photoset"]["photo"];

	echo "
<ul>";

	foreach($photos as $photo) {

               $farm              = $photo['farm'];
               $server            = $photo['server'];
               $photo_id          = $photo['id'];
               $secret            = $photo['secret'];
               $photo_title       = $photo['title'];
<li><img /*SEE CODE BLOCK BELOW/* /></li>

	}
	  echo "</li>
</ul>

";

	} else {
            echo "Error getting photos";
       }</pre>
<p>     [note: the codeblock is not displaying the following img src line correctly in the line above so it has been moved below]</p>
<pre name="code" class="php">src="http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'].'/'.$photo['id'].'_'.$photo['secret'].'_t.jpg" alt="'.$photo['title'].'"</pre>
<p>The code above first checks to see if the data was returned ok before trying to iterate through the array. Once we have verified that we have the data, we name the <code>[photo]</code> array more intuitively.</p>
<h3 class="blog_ttl">Putting it all together</h3>
<p>Let&#8217;s break down the URL piece by piece to better understand whats going on: For example if the URL is as follows:</p>
<p>http://farm<strong>3</strong>.static.flickr.com/<strong>2421</strong>/<strong>4002906997</strong>_<strong>5e9de854c6</strong>_<strong>t</strong>.jpg</p>
<ul>
<li>$photo["farm"] = 3</li>
<li>$photo["server"] = 2421</li>
<li>$photo["id"] = 4002906997</li>
<li>$photo["secret"] = 5e9de854c6</li>
<li>_s = 75px square thumbnail</li>
<li>_t = 100px thumbnail</li>
<li>_m = 240px thumbnail</li>
</ul>
<p><strong>To get the &#8220;normal&#8221; 500px image, use the sequence above minus the &#8220;underscore letter&#8221; ending.</strong></p>
<p><strong>To get the full &#8220;original size&#8221; takes a bit more work:</strong></p>
<p><strong><br />
</strong></p>
<pre name="code" class="php">src="http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'].'/'.$photo['id'].'_'.$photo['originalsecret'].'_o.jpg"</pre>
<p><strong>UPDATE 7/10:</strong> I realized that I haven&#8217;t updated this in a while and wanted to share a much easier way to get the original full-size image:</p>
<pre name="code" class="php">
	// In the params array, replace:
	'extras'	=> 'original_format',

	// With:
	'extras'	=> 'url_o',

	//And to call the image:

	src="'.$photo['url_o'].'"
</pre>
<p>You should now be able to create a sample image page from a photo set in all sizes. You can also apply these principles to almost any of Flickr&#8217;s API Methods to achieve more customized results. Enjoy!</p>
<p><a class="view_tutorial ext" href="http://darkcrimson.com/flickr_example.php">View thumbnail Sample Page</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darkcrimson.com/2009/11/mastering-the-flickr-api-with-php-and-curl/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

