<?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>Cagintranet Web Design &#187; API</title>
	<atom:link href="http://www.cagintranet.com/cat/api/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cagintranet.com</link>
	<description>Pittsburgh Web Design Â» Cagintranet Web Design - Web Designer, Developer, Graphic Artist and Web 2.0 Guru</description>
	<lastBuildDate>Thu, 25 Mar 2010 10:32:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Create Google Static Area Maps with Latitude &amp; Longitude Boundaries</title>
		<link>http://www.cagintranet.com/archive/google-static-area-maps-latitude-longitude/</link>
		<comments>http://www.cagintranet.com/archive/google-static-area-maps-latitude-longitude/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 18:17:49 +0000</pubDate>
		<dc:creator>Chris Cagle</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.cagintranet.com/?p=667</guid>
		<description><![CDATA[<a href="http://www.cagintranet.com/archive/google-static-area-maps-latitude-longitude/"><img align="left" hspace="5" width="250" src="http://maps.google.com/maps/api/staticmap?path=color:blue|weight:4|fillcolor:black|40.5059,-80.0614|40.5123,-80.0504|40.5062,-80.0453|40.5066,-80.0336|40.5023,-80.0337|40.4999,-80.0456|40.4885,-80.0452|40.4837,-80.0518|40.5023,-80.0791|40.5151,-80.1205|40.531,-80.11|40.5283,-80.1076|40.5271,-80.111|40.5208,-80.1056|40.5133,-80.0926|40.5178,-80.0932|40.5212,-80.0879|40.5205,-80.0753|40.5233,-80.0718|40.5201,-80.0611|40.5059,-80.0614&size=500x300&sensor=false&key=ABQIAAAA_6h1iP7xkNWbJaJtdQExbxQoasim_Ec9007jwQbCPHO_oeeE-RTam6RjbglJrcjrbo6lqh5Wmpn8uQ" class="alignleft wp-post-image tfe" alt="" title="" /></a>This tutorial shows you how to use the little known Google Static Maps feature to outline a specific boundary such a zip code or a city. It requires that you have your own Google Maps API. Above is the output image generated by the code below. This is the PHP code that is needed. The [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://maps.google.com/maps/api/staticmap?path=color:blue|weight:4|fillcolor:black|40.5059,-80.0614|40.5123,-80.0504|40.5062,-80.0453|40.5066,-80.0336|40.5023,-80.0337|40.4999,-80.0456|40.4885,-80.0452|40.4837,-80.0518|40.5023,-80.0791|40.5151,-80.1205|40.531,-80.11|40.5283,-80.1076|40.5271,-80.111|40.5208,-80.1056|40.5133,-80.0926|40.5178,-80.0932|40.5212,-80.0879|40.5205,-80.0753|40.5233,-80.0718|40.5201,-80.0611|40.5059,-80.0614&#038;size=500x300&#038;sensor=false&#038;key=ABQIAAAA_6h1iP7xkNWbJaJtdQExbxQoasim_Ec9007jwQbCPHO_oeeE-RTam6RjbglJrcjrbo6lqh5Wmpn8uQ" alt="" /></p>
<p>This tutorial shows you how to use the little known <a href="http://code.google.com/apis/maps/documentation/staticmaps/">Google Static Maps</a> feature to outline a specific boundary such a zip code or a city. It requires that you have your own <a href="http://code.google.com/apis/maps/">Google Maps API</a>. Above is the output image generated by the code below.</p>
<p>This is the PHP code that is needed. The tricky part is finding the latitude and longitude points of the particular area you want to draw. I got my data for <a href="http://buyapittsburghhome.com/">BuyAPittsburghHome.com</a> systematically from <a href="http://www.pushpin.com/">PushPin&#8217;s API</a>.</p>
<h3>The PHP</h3>
<pre><code class="php">
# Google Maps API Key (replace with your key)
$apikey="ABQIAAAA_6h1iP7xkNWbJaJtdQExbxQoasim_Ec9007jwQbCPHO_oeeE-RTam6RjbglJrcjrbo6lqh5Wmpn8uQ";

# $latlon is a variable pulled from MySQL that stores all the latitude and longitude points of a particular area.
# In your case, you would probably have a database connection followed by sql that pulls the respective data needed.
# Since I cannot tell you where/how to pull the data from your own database, I am just going to set it here manually.
$latlon = 'a:21:{i:0;a:2:{i:0;s:8:"-80.0614";i:1;s:7:"40.5059";}i:1;a:2:{i:0;s:8:"-80.0504";i:1;s:7:"40.5123";}i:2;a:2:{i:0;s:8:"-80.0453";i:1;s:7:"40.5062";}i:3;a:2:{i:0;s:8:"-80.0336";i:1;s:7:"40.5066";}i:4;a:2:{i:0;s:8:"-80.0337";i:1;s:7:"40.5023";}i:5;a:2:{i:0;s:8:"-80.0456";i:1;s:7:"40.4999";}i:6;a:2:{i:0;s:8:"-80.0452";i:1;s:7:"40.4885";}i:7;a:2:{i:0;s:8:"-80.0518";i:1;s:7:"40.4837";}i:8;a:2:{i:0;s:8:"-80.0791";i:1;s:7:"40.5023";}i:9;a:2:{i:0;s:8:"-80.1205";i:1;s:7:"40.5151";}i:10;a:2:{i:0;s:6:"-80.11";i:1;s:6:"40.531";}i:11;a:2:{i:0;s:8:"-80.1076";i:1;s:7:"40.5283";}i:12;a:2:{i:0;s:7:"-80.111";i:1;s:7:"40.5271";}i:13;a:2:{i:0;s:8:"-80.1056";i:1;s:7:"40.5208";}i:14;a:2:{i:0;s:8:"-80.0926";i:1;s:7:"40.5133";}i:15;a:2:{i:0;s:8:"-80.0932";i:1;s:7:"40.5178";}i:16;a:2:{i:0;s:8:"-80.0879";i:1;s:7:"40.5212";}i:17;a:2:{i:0;s:8:"-80.0753";i:1;s:7:"40.5205";}i:18;a:2:{i:0;s:8:"-80.0718";i:1;s:7:"40.5233";}i:19;a:2:{i:0;s:8:"-80.0611";i:1;s:7:"40.5201";}i:20;a:2:{i:0;s:8:"-80.0614";i:1;s:7:"40.5059";}}';

# unserialize the code taken from your mySQL database. If your data is not serialized, comment this line out
$bounds = unserialize($latlon);

if ($bounds) {
	# cycle thru each lat/lon pair, concatenate and make pipe-delimited
	foreach ($bounds as $point) {
 		$latlon .= "%7C".$point[1].",".$point[0];
	}

	# build the image url
	$path = "color:blue|weight:4|fillcolor:black". $latlon;
	$MapAreaImage = "http://maps.google.com/maps/api/staticmap?path=".$path."&#038;size=500x300&#038;sensor=false&#038;key=".$apikey;
}
</code></pre>
<h3>The HTML</h3>
<p>This is the code that then displays that drawn out area map image in HTML:</p>
<pre><code class="html">&lt;img src="&lt;?php echo $MapAreaImage; ?>" alt="" /></code></pre>
<h3>The Output</h3>
<p>The PHP and HTML combined will generate the code below as the image source. A live example of this is the header image of this post (Zip Code 15202 in Pittsburgh, PA).</p>
<pre><code class="html">&lt;img src="http://maps.google.com/maps/api/staticmap?path=color:blue|weight:4|fillcolor:black|40.5059,-80.0614|40.5123,-80.0504|40.5062,-80.0453|40.5066,-80.0336|40.5023,-80.0337|40.4999,-80.0456|40.4885,-80.0452|40.4837,-80.0518|40.5023,-80.0791|40.5151,-80.1205|40.531,-80.11|40.5283,-80.1076|40.5271,-80.111|40.5208,-80.1056|40.5133,-80.0926|40.5178,-80.0932|40.5212,-80.0879|40.5205,-80.0753|40.5233,-80.0718|40.5201,-80.0611|40.5059,-80.0614&#038;size=500x300&#038;sensor=false&#038;key=ABQIAAAA_6h1iP7xkNWbJaJtdQExbxQoasim_Ec9007jwQbCPHO_oeeE-RTam6RjbglJrcjrbo6lqh5Wmpn8uQ" alt="" /></code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cagintranet.com/archive/google-static-area-maps-latitude-longitude/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Create your own BlogCatalog Widget via their API</title>
		<link>http://www.cagintranet.com/archive/how-to-create-your-own-blogcatalog-widget-via-their-api/</link>
		<comments>http://www.cagintranet.com/archive/how-to-create-your-own-blogcatalog-widget-via-their-api/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 04:00:25 +0000</pubDate>
		<dc:creator>Chris Cagle</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.cagintranet.com/?p=248</guid>
		<description><![CDATA[<a href="http://www.cagintranet.com/archive/how-to-create-your-own-blogcatalog-widget-via-their-api/"><img align="left" hspace="5" width="250" src="http://www.cagintranet.com/design/wp-content/uploads/2008/07/bc-main.jpg" class="alignleft wp-post-image tfe" alt="" title="Blog Catalog" /></a>When releasing the first version of my WP-SocialCount WordPress plugin a while back, an interested visitor asked if I would be so kind as to include a way to reshape the BlogCatalog widget with the next release of the plugin. I had every intention of doing that until I saw just how much would have [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.cagintranet.com/design/wp-content/uploads/2008/07/bc-main.jpg" alt="" title="Blog Catalog" width="500" height="171" class="alignnone size-full wp-image-254" /></p>
<p>When releasing the first version of my <a href="http://www.cagintranet.com/archive/wp-socialcount-plugin/">WP-SocialCount WordPress plugin</a> a while back, <a href="http://45caliberweb.com/">an interested visitor</a> asked if I would be so kind as to include a way to reshape the <a href="http://www.blogcatalog.com/">BlogCatalog</a> widget with the next release of the plugin. I had every intention of doing that until I saw just how much would have to be changed in my plugin to accommodate this &#8211; so I decided to write this instead.</p>
<h3>Current BlogCatalog Widgets</h3>
<p>Currently, the list of <a href="http://www.blogcatalog.com/buttons.php">widgets that BlogCatalog provides</a> it&#8217;s users is pretty meager. Hopefully this tutorial will allow it&#8217;s members to promote themselves a little easier (and in better style!) than before.</p>
<p><img src="http://www.cagintranet.com/design/wp-content/uploads/2008/07/bc-exitingwidgetsgif.gif" alt="" title="BC\&#039;s Exiting Widgets" width="500" height="348" class="alignnone size-full wp-image-253" /></p>
<h3>Step 1: Get your API Key</h3>
<p>To get your API key, all you need to do is <a href="http://www.blogcatalog.com/">sign in</a>, and go to it&#8217;s <a href="http://www.blogcatalog.com/api/">developers page</a>. A couple sections down, you will see something like the image below. Write down your key and save it for later.</p>
<p><img src="http://www.cagintranet.com/design/wp-content/uploads/2008/07/bcapi-key.gif" alt="" title="Get the API Key" width="500" height="100" class="alignnone size-full wp-image-252" /></p>
<h3>Step 2: Construct the API Calls</h3>
<p>In order to make the API call, we need to know the structure that BlogCatalog requires the calls to be in. Looking at their <a href="http://www.blogcatalog.com/api/">developers page</a>, we see that they only have two types of calls. One is for gathering <strong>User</strong> data and the other is for getting <strong>Blog</strong> data.</p>
<p>We are going to start off using the User data api call because this should give us quite a bit of the information we need right off the bat.</p>
<p>The structure as outlined by their documentation is: <strong>http://api.blogcatalog.com/getinfo?bcwsid=[apikey]&#038;username=[username]</strong></p>
<p>Looking over this call I see that the only thing we don&#8217;t currently have is the <strong>[username]</strong>. Remember, we got the <strong>[apikey]</strong> back in Step 1.</p>
<h3>Step 3: Set Initial Variables</h3>
<p>To make our coding as easy as possible, lets set some variables right off the bat so that we can reference them anytime we need their data later on.</p>
<p><em><strong>FYI:</strong> I will be adding each new step on top of each previous step so that you can see how we build the application&#8230;</em></p>
<pre><code class="php">//Setting variables
$api = &quot;xxxxxxxxxx&quot;;		//Your API key
$username = &quot;ccagle8&quot;; 	//Your username</code></pre>
<p>FYI: I X&#8217;d out my api key, but you can easily insert your own key in here&#8230;</p>
<h3>Step 4: Use PHP to Capture Your Data</h3>
<p>Since BlogCatalog returns all their data in the form of XML (their documentation didn&#8217;t state this, but it&#8217;s the format their examples are in, so let&#8217;s run with that) we will use the <a href="http://www.php.net/simplexml_load_file">simplexml_load_file</a> php function to load the file into the variable <strong>$userfeeddata</strong>.</p>
<pre><code class="php">//Setting variables
$api = &quot;xxxxxxxxxx&quot;;		//Your API key
$username = &quot;ccagle8&quot;; 	//Your username

//Pulling API user feed data
$userfeeddata = simplexml_load_file (&quot;http://api.blogcatalog.com/getinfo?bcwsid=$api&amp;username=$username&quot;);</code></pre>
<h3>Step 5: Pull That Data Into Variables</h3>
<p>After loading that file of data into our <strong>$userfeeddata</strong> variable, we can start pulling the information we need out. You can see below that I am pulling the <em>avatar</em> and <em>url</em> from this data. How I realize the hierarchy of the data is done by doing a <a href="http://www.php.net/print_r">print_r</a> command on the <strong>$userfeeddata</strong> variable. </p>
<pre><code class="php">print_r ($userfeeddata);</code></pre>
<p>I usually add this line of code in right after we load the data, and then comment it out once I have everything that I need. After analyzing the print_r output, this is what I came up with:</p>
<pre><code class="php">//Setting variables
$api = &quot;xxxxxxxxxx&quot;;		//Your API key
$username = &quot;ccagle8&quot;; 	//Your username

//Pulling API user feed data
$userfeeddata = simplexml_load_file (&quot;http://api.blogcatalog.com/getinfo?bcwsid=$api&amp;username=$username&quot;);
//print_r ($userfeeddata);

//Loading that data into variables for later
$avatar = $userfeeddata-&gt;result-&gt;avatar;
$url = $userfeeddata-&gt;result-&gt;weblogs-&gt;weblog-&gt;url;</code></pre>
<h3>Step 6: Use this Data for the Second of our API Calls</h3>
<p>back in Step 2 we found that BC gives us two types of API calls. We already used the User data feed, but now we will make use of the Blog data feed: <strong>http://api.blogcatalog.com/bloginfo?bcwsid=[apikey]&#038;url=[blog url]</strong> </p>
<p>Just as before, we already have the <strong>[apikey]</strong> value, but we don&#8217;t yet have the <strong>[blog url]</strong>. Actually we do, and it is in the form of <strong>$url</strong>, which we got in Step 5. So, lets make another call to the API, only this time using the one to get Blog information: </p>
<pre><code class="php">//Setting variables
$api = &quot;xxxxxxxxxx&quot;;		//Your API key
$username = &quot;ccagle8&quot;; 	//Your username

//Pulling API user feed data
$userfeeddata = simplexml_load_file (&quot;http://api.blogcatalog.com/getinfo?bcwsid=$api&amp;username=$username&quot;);

//Loading that data into variables for later
$avatar = $userfeeddata-&gt;result-&gt;avatar;
$url = $userfeeddata-&gt;result-&gt;weblogs-&gt;weblog-&gt;url;

//Pulling API blog feed data
$blogfeeddata = simplexml_load_file (&quot;http://api.blogcatalog.com/bloginfo?bcwsid=$api&amp;url=$url&quot;);</code></pre>
<p>Using the print_r command again, I find a some more data that I want to pull out to include in our new widget.</p>
<pre><code class="php">//Setting variables
$api = &quot;xxxxxxxxxx&quot;;		//Your API key
$username = &quot;ccagle8&quot;; 	//Your username

//Pulling API user feed data
$userfeeddata = simplexml_load_file (&quot;http://api.blogcatalog.com/getinfo?bcwsid=$api&amp;username=$username&quot;);

//Loading that data into variables for later
$avatar = $userfeeddata-&gt;result-&gt;avatar;
$url = $userfeeddata-&gt;result-&gt;weblogs-&gt;weblog-&gt;url;

//Pulling API blog feed data
$blogfeeddata = simplexml_load_file (&quot;http://api.blogcatalog.com/bloginfo?bcwsid=$api&amp;url=$url&quot;);

//Loading that data into variables for later
$tags = $blogfeeddata-&gt;result-&gt;weblog-&gt;tags-&gt;tag;
$categories = $blogfeeddata-&gt;result-&gt;weblog-&gt;categories-&gt;category;
$bcurl = $blogfeeddata-&gt;result-&gt;weblog-&gt;bcurl;
$views = $blogfeeddata-&gt;result-&gt;weblog-&gt;views;
$rank = $blogfeeddata-&gt;result-&gt;weblog-&gt;rank;
$friends = $blogfeeddata-&gt;result-&gt;weblog-&gt;neighborhood-&gt;user;
$viewers = $blogfeeddata-&gt;result-&gt;weblog-&gt;recent_viewers-&gt;user;</code></pre>
<h3>Step 7. Echoing the Data back to the Page</h3>
<p>We have all our data mining complete, so now we can focus on sending this it out to our web page.</p>
<p>Here we are echoing out some of the user information that we received during the two API calls we did. You can see that we are showing your Avatar, which is linked to your BlogCatalog page, and your site&#8217;s number of views and rank. </p>
<pre><code class="php">//Echos out User Information
echo &quot;&lt;a href=\&quot;&quot;. $bcurl .&quot;\&quot;&gt;&lt;img src=\&quot;&quot;. $avatar .&quot;\&quot; alt=\&quot;View &quot;. $username .&quot;&#39;s BlogCatalog Profile\&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&quot;;
echo &quot;Views: &quot;. $views .&quot;&lt;br /&gt;&quot;;
echo &quot;Rank: &quot;. $rank .&quot;&lt;br /&gt;&quot;;</code></pre>
<p>The next data we want to show are the tags and categories of the blog in question. To do this, we can use a <a href="http://www.php.net/foreach">foreach</a> loop that will cycle through each tag and category and create a link back to the BlogCatalog page for each one. </p>
<p><strong>Hint:</strong> You can see that I had to sanitize the variables by converting all letters to lowercase and replace spaces with underscores in order for them to work correctly in the URL.</p>
<pre><code class="php">//Cycle through and display tags
	echo &quot;Tags:&quot;;
	$set = 0;
	foreach ($tags as $i =&gt; $tag) {
		if($set &lt; $limit){
			echo &quot;&lt;a href=\&quot;http://www.blogcatalog.com/tag/&quot;. strtolower($tag) . &quot;\&quot;&gt;&quot;. $tag .&quot;&lt;/a&gt; &quot;;
		}
		$set  ;
	}

	echo &quot;&lt;br /&gt;&quot;;

	//Cycle through and display categories
	echo &quot;Categories: &quot;;
	$set = 0;
	foreach ($categories as $i =&gt; $cat) {
		if($set &lt; $limit){
			echo &quot;&lt;a href=\&quot;http://www.blogcatalog.com/directory/&quot;. urlencode(strtolower(str_replace(&quot; &quot;, &quot;_&quot;, &quot;$cat&quot;))) . &quot;\&quot;&gt;&quot;. $cat .&quot;&lt;/a&gt; &quot;;
		}
		$set  ;
	}
</code></pre>
<p><strong>DEMO: </strong><a href="http://www.cagintranet.com/code/blog-catalog">View the final outcome of this tutorial</a> (with a little bit of CSS and HTML markup added, which you can see in the header section of the page when you do a &#8220;view source&#8221;).</p>
<p><strong>SOURCE CODE:</strong> Download the <a href='http://www.cagintranet.com/design/wp-content/uploads/2008/07/blogcatalog.phps'>full PHP source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cagintranet.com/archive/how-to-create-your-own-blogcatalog-widget-via-their-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a Simple Online Application using the Flickr API</title>
		<link>http://www.cagintranet.com/archive/creating-a-simple-php-application-using-the-flickr-api/</link>
		<comments>http://www.cagintranet.com/archive/creating-a-simple-php-application-using-the-flickr-api/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 04:00:40 +0000</pubDate>
		<dc:creator>Chris Cagle</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.cagintranet.com/?p=89</guid>
		<description><![CDATA[<a href="http://www.cagintranet.com/archive/creating-a-simple-php-application-using-the-flickr-api/"><img align="left" hspace="5" width="250" src="http://www.cagintranet.com/design/wp-content/uploads/2008/06/mainphoto.jpg" class="alignleft wp-post-image tfe" alt="" title="A Flickr API Tutorial" /></a>Photo Credit: *dans The Flickr API is one of the most widely used APIs out there. In this article I will show you how to build your own simple online application using PHP and the Flickr API. The sample online application we build today will be one that takes a Flickr image URL (e.g. http://www.flickr.com/photos/dans180/854998249/), [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.cagintranet.com/design/wp-content/uploads/2008/06/mainphoto.jpg" alt="" title="A Flickr API Tutorial" width="499" height="231" class="alignnone size-full wp-image-92" /></p>
<p><cite class="flickrcite"><br />
	Photo Credit: <a href="http://www.flickr.com/photos/dans180/854998249/">*dans</a><br />
</cite></p>
<p>The <a href="http://www.flickr.com/services/api/">Flickr API</a> is one of the most widely used APIs out there. In this article I will show you how to build your own simple online application using PHP and the Flickr API.</p>
<p>The sample online application we build today will be one that takes a Flickr image URL (e.g. <em>http://www.flickr.com/photos/dans180/854998249/</em>), then outputs the code needed for you to properly display the photo&#8217;s credit. Remember, every time you use a Creative Commons licensed photo from Flickr, you <a href="http://www.flickr.com/guidelines.gne">need to provide the proper attribution</a> and that is usually considered a link back to the photo while mentioning the photographer&#8217;s name. This is an example of the kind of output that we hope to get:</p>
<p><!--insert link-example here --></p>
<p><strong>Real Link: </strong><br />
Photo Credit: <a href="http://www.flickr.com/photos/dans180/854998249/">*dans</a></p>
<p><strong>HTML Code Output:</strong></p>
<pre><code class="html">Photo Credit: &lt;a href=&quot;http://www.flickr.com/photos/dans180/854998249/&quot;&gt;*dans&lt;/a&gt;</code></pre>
<h3>Getting Started</h3>
<p>First off, you will need to get an API key. <a href="http://www.flickr.com/services/api/keys/apply/">Get one here</a> and hold onto it for later.</p>
<p><img src="http://www.cagintranet.com/design/wp-content/uploads/2008/06/apikey.jpg" alt="" title="Get Your API Key" width="499" height="231" class="alignnone size-full wp-image-91" /></p>
<p>Next, we need to build the form that our visitors can input the URL into. This should be pretty basic stuff for most of us.</p>
<h3>The HTML Form</h3>
<p><!--insert form-code here --></p>
<pre><code class="html">&lt;form method=&quot;post&quot; id=&quot;form&quot; action=&quot;flickr_api_example.php&quot; &gt;
	&lt;table&gt;
		&lt;tr&gt;
		&lt;td&gt;&lt;label for=&quot;photo&quot;&gt;Flickr Photo URL:&lt;/label&gt;&lt;/td&gt;
		&lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;&quot; size=&quot;50&quot; name=&quot;photo&quot; id=&quot;photo&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
		&lt;td&gt;&lt;/td&gt;
		&lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;Generate Code&quot; name=&quot;submitted&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
&lt;/form&gt;</code></pre>
<h3>The PHP if-statement</h3>
<p>This form is setup to POST the data back to itself for processing. To handle this, we need to make use of an if-statement to check to see if the form is submitting data back to itself, or if it just needs to show a blank form to a new visitor. </p>
<p>The PHP evaluation <a href="http://us3.php.net/isset">isset</a> allows us to determine whether or not a variable is set. In our example, we check to see if the variable &#8216;<em>photo</em>&#8216; is set &#8211; because that is the name of the input field where the visitor enters the Flickr image&#8217;s URL.</p>
<p><!-- insert if-statement-setup here --></p>
<pre><code class="php">&lt;?php
if (isset($_POST[&#39;photo&#39;])) { //The form was submitted, process the data

} else { //The form was not submitted

};
?&gt;</code></pre>
<p>If the variable &#8216;<em>photo</em>&#8216; is set, the next step is to initialize some variables. The first variable is the image URL that the user submitted into our form, while the second will be the API key we got from Flickr.</p>
<p><!-- insert if-statement-variables here --></p>
<pre><code class="php">&lt;?php
if (isset($_POST[&#39;photo&#39;])) { //The form was submitted, process the data
	$queryString=$_POST[&#39;photo&#39;]; //The URL submitted
	$flickr_api_key = &quot;352ee7XXXXXXXXXXXXXXXXXXXXXXXXXX&quot;; //Our API Key

} else { //The form was not submitted

};
?&gt;</code></pre>
<p>In order to make the URL submitted usable to us, we need to split it up into its relevant sections. I use the PHP function <a href="http://us2.php.net/explode">explode</a> to help break the URL up by the character &#8216;/&#8217;. All we need from this URL is the photo id, and we can see below is the 6th of these so called sections.</p>
<p><em><strong>PHP FACT:</strong> PHP starts it&#8217;s count with 0.</em></p>
<p><img src="http://www.cagintranet.com/design/wp-content/uploads/2008/06/print_r-url.gif" alt="" title="print_r output of $url" width="500" height="153" class="alignnone size-full wp-image-90" /></p>
<p><!-- insert if-statement-spliturl here --></p>
<pre><code class="php">&lt;?php
if (isset($_POST[&#39;photo&#39;])) { //The form was submitted, process the data
	$queryString=$_POST[&#39;photo&#39;]; //The URL submitted
	$flickr_api_key = &quot;352ee7XXXXXXXXXXXXXXXXXXXXXXXXXX&quot;; //Our API Key

	$url = explode(&quot;/&quot;, $queryString); //Split the URL up into sections, delimited by the character '/'
	$photo_id = $url[5]; //The Photo ID is the 6th of these sections

} else { //The form was not submitted

};
?&gt;</code></pre>
<h3>Structuring the Flickr API</h3>
<p>After turning the contents of the <em>photo id</em> section into it&#8217;s own variable, we now have everything we need to start the API call. </p>
<p>After searching the Flickr site a while, I found the <a href="http://www.flickr.com/services/api/flickr.photos.getInfo.html">flickr.photos.getInfo</a> method &#8211; and judging by the example it displays, it looks to be exactly what we want. Notice we already have both of the required variables to make this call: <strong>$photo_id</strong> and <strong>$api_key</strong>. </p>
<p>Next, we need to find out what the correct call syntax that Flickr requires in order to get data back from their API. Luckily, they are nice enough to give us an <a href="http://www.flickr.com/services/api/response.php.html">example PHP call here</a>. This test gives us the insight needed as to what an API call should be structured like: </p>
<p><!-- insert if-statement-flickrapicall here --></p>
<p><strong>http://www.flickr.com/services/rest/?method=flickr.test.echo&#038;format=php_serial&#038;api_key=eaab5c513e33c01544385641ff9af42f</strong></p>
<p>After we modify their example to use the method <strong>flickr.photos.getInfo</strong>, we also need insert a new argument for the <em>photo_id</em>. Our newly structured API call now looks like this: <em>(changes are in red)</em></p>
<p><!-- insert if-statement-flickrapicall here --></p>
<p><strong>http://www.flickr.com/services/rest/?method=<em style="color:#cc0000;">flickr.photos.getInfo</em>&#038;format=php_serial<em style="color:#cc0000;">&#038;photo_id=$photo_id</em>&#038;api_key=<em style="color:#cc0000;">$flickr_api_key</em></strong></p>
<p>You can test the validity of our newly structured call by pasting it into any web browser&#8217;s address bar. You will get back real data if it works, or <a href="http://www.flickr.com/services/rest/?method=flickr.blogs.getList&#038;format=php_serial&#038;api_key=eaab5c513e33c01544385641ff9af42f">errors when it fails</a>.</p>
<h3>Using the Data from the Flickr API</h3>
<p>Once we get a working API call, we need to have our application read in the data that is returned. To import this data into our web app, we use the function <a href="http://us3.php.net/file_get_contents">file_get_contents</a>, then re-format it using the <a href="http://us3.php.net/manual/en/function.unserialize.php">unserialize function</a>. </p>
<p><!-- insert if-statement-flickrapicall here --></p>
<pre><code class="php">&lt;?php
if (isset($_POST[&#39;photo&#39;])) { //The form was submitted, process the data
	$queryString=$_POST[&#39;photo&#39;]; //The URL submitted
	$flickr_api_key = &quot;352ee7XXXXXXXXXXXXXXXXXXXXXXXXXX&quot;; //Our API Key

	$url = explode(&quot;/&quot;, $queryString); //Split the URL up into sections, delimited by the character/
	$photo_id = $url[5]; //The Photo ID is the 6th of these sections

	$flickrrawoutput =  file_get_contents(&quot;http://flickr.com/services/rest/?method=flickr.photos.getInfo&amp;photo_id=$photo_id&amp;api_key=$flickr_api_key&amp;format=php_serial&quot;);
	$flickroutput = unserialize($flickrrawoutput);

} else { //The form was not submitted

};
?&gt;</code></pre>
<p>Since all good coders do error checking, we also need to run a quick check to see if the API call returned no data (an error). We can do so with this simple line:</p>
<p><!-- insert if-statement-flickrerrorline here --></p>
<pre><code class="php">$status = $flickroutput[&#39;stat&#39;]; //Extracts the &#39;stat&#39; value from the data returned</code></pre>
<p>Now, with a second if-statement, we check to see the variable <strong>$status</strong>&#8216;s value, and if it doesn&#8217;t contain the word &#8216;<em>fail</em>&#8216; then we know we made a successful call. Putting it all together, it looks like this:</p>
<p><!-- insert if-statement-flickrsecondif here --></p>
<pre><code class="php">&lt;?php
if (isset($_POST[&#39;photo&#39;])) { //The form was submitted, process the data
	$queryString=$_POST[&#39;photo&#39;]; //The URL submitted
	$flickr_api_key = &quot;352ee7XXXXXXXXXXXXXXXXXXXXXXXXXX&quot;; //Our API Key

	$url = explode(&quot;/&quot;, $queryString); //Split the URL up into sections, delimited by the character/
	$photo_id = $url[5]; //The Photo ID is the 6th of these sections

	//Get the data from Flickr
	$flickrrawoutput =  file_get_contents(&quot;http://flickr.com/services/rest/?method=flickr.photos.getInfo&amp;photo_id=$photo_id&amp;api_key=$flickr_api_key&amp;format=php_serial&quot;);
	$flickroutput = unserialize($flickrrawoutput); 

	$status = $flickroutput[&#39;stat&#39;]; //Extracts the &#39;stat&#39; value from the data returned

	if ($status != &#39;fail&#39;) { //fetch from Flickr returned stats for photo

	} else {
		echo &quot;The supplied photo URL is wrong. Please &lt;a href=\&quot;#form\&quot;&gt;try again&lt;/a&gt;.&quot;; //A bad URL was entered into the form
	};

} else { //The form was not submitted

};
?&gt;</code></pre>
<p>Once we know we&#8217;ve made a successful API call, we need to extract the &#8216;<em>username</em>&#8216; of the photo&#8217;s owner. We do this with this line:</p>
<p><!-- insert flickr-extract-data --></p>
<pre><code class="php">$flickr_user  = $flickroutput[&#39;photo&#39;][&#39;owner&#39;][&#39;username&#39;];</code></pre>
<p>&#8230; but this is sometimes not an easy thing to figure out. I have found the best way to find out what to extract is by analyzing the unserialized data we get back from Flickr. PHP allows us to look at the data in an easier to read format by using <a href="http://us3.php.net/manual/en/function.print-r.php">print_r</a>.</p>
<p><!-- insert flickr-print_r --></p>
<pre><code class="php">print_r ($flickroutput)</code></pre>
<p>This is probably the hardest part of using the API. If you insert the <em>print_r</em> command into your PHP document right after we unserialize the Flickr data, we can easily see the different levels of the data structure. </p>
<p><em>Hint: You can view the output of <a href="http://www.cagintranet.com/design/wp-content/uploads/2008/06/print_r.php">our print_r command here</a>. The different tiers are highlighted in red.</em></p>
<p>The way our data extract works is &#8216;photo&#8217; is the first level within the <strong>$flickroutput</strong> data. The next tier down is &#8216;owner&#8217;, which houses the last level called &#8216;username&#8217; which holds the data we need. That&#8217;s how we get <strong>$flickroutput['photo']['owner']['username']</strong> &#8212; each [] is referencing it&#8217;s own tier.</p>
<h3>Output the Data to the Visitor</h3>
<p>Once we have the <strong>$flickr_user</strong> variable set with this extract, we are ready to output our finished code. We will output it twice: One as an example &#8220;live&#8221; link, and the other will be the raw HTML code that the visitor can input into his or her site. </p>
<p><!-- insert if-statement-final here --></p>
<pre><code class="php">&lt;?php
if (isset($_POST[&#39;photo&#39;])) { //The form was submitted, process the data
	$queryString=$_POST[&#39;photo&#39;]; //The URL submitted
	$flickr_api_key = &quot;352ee7XXXXXXXXXXXXXXXXXXXXXXXXXX&quot;; //Our API Key

	$url = explode(&quot;/&quot;, $queryString); //Split the URL up into sections, delimited by the character/
	$photo_id = $url[5]; //The Photo ID is the 6th of these sections

	//Get the data from Flickr
	$flickrrawoutput =  file_get_contents(&quot;http://flickr.com/services/rest/?method=flickr.photos.getInfo&amp;photo_id=$photo_id&amp;api_key=$flickr_api_key&amp;format=php_serial&quot;);
	$flickroutput = unserialize($flickrrawoutput); 

	$status = $flickroutput[&#39;stat&#39;]; //Extracts the &#39;stat&#39; value from the data returned

	if ($status != &#39;fail&#39;) { //fetch from Flickr returned stats for photo
		$flickr_user  = $flickroutput[&#39;photo&#39;][&#39;owner&#39;][&#39;username&#39;];
	?&gt;

	&lt;p&gt;Preview: &lt;/p&gt;
	&lt;cite&gt;Photo Credit: &lt;a href=&quot;&lt;?php echo $queryString; ?&gt;&quot; title=&quot;Photo Credit: &lt;?php echo $flickr_user; ?&gt;&quot;&gt;&lt;?php echo $flickr_user; ?&gt;&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
	&lt;p&gt;HTML Code:&lt;/p&gt;
	&lt;pre&gt;
		&amp;lt;cite&amp;gt;
			Photo Credit: &amp;lt;a href=&amp;quot;&lt;?php echo $queryString; ?&gt;&amp;quot; title=&amp;quot;Photo Credit: &lt;?php echo $flickr_user; ?&gt;&amp;quot;&amp;gt;&lt;?php echo $flickr_user; ?&gt;&amp;lt;/a&amp;gt;
		&amp;lt;/cite&amp;gt;
	&lt;/pre&gt;

	&lt;?php
	} else {
		echo &quot;The supplied photo URL is wrong. Please &lt;a href=\&quot;#form\&quot;&gt;try again&lt;/a&gt;.&quot;; //A bad URL was entered into the form
	};

} else { //The form was not submitted

};
?&gt;</code></pre>
<h3>Putting it all Together</h3>
<p>Now that we have the form and the PHP sections completed, we need to put it all together into an application page. You can download the <a href="http://www.cagintranet.com/design/wp-content/uploads/2008/06/flickr_api_example.phps">example page here</a>, or you can take a look at the <a href="http://www.cagintranet.com/code/flickrcite/">working example I&#8217;ve put up</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cagintranet.com/archive/creating-a-simple-php-application-using-the-flickr-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
