Blog
Using the Flickr API to retrieve a list of photosets
Lots of good work has been done on the Flickr API with ColdFusion already: specifically, CFlickr - see http://chris.m0nk3y.net/projects/CFlickr/.
However, for my uses, I didn't need anything near as complicated: All I wanted to do was to pull a list of photosets, their titles, primary thumbnail and description.
Thankfully, this is quite easy due to the Flickr API.
Part One: Making the request to Flickr
I'm assuming you have an API code, and know your userID:
<!---Flickr Params--->
<cfparam name="flickr" default="http://www.flickr.com/services/rest/" />
<cfparam name="key" default="Your API Key Here" />
<cfparam name="userid" default="Your User ID Here">
<!---url.reinit used to manually refresh-->
<cfparam name="url.reinit" default="">
<!--- Do we have this value? --->
<cfif not isDefined("application.FlickrXml") OR len(url.reinit)>
<!---Send my Request to Flickr: this will get an XML doc with all my public photosets--->
<cfhttp url="#flickr#">
<cfhttpparam name="api_key" type="url" value="#key#" />
<cfhttpparam name="method" type="url" value="flickr.photosets.getList" />
<cfhttpparam name="user_id" type="url" value="#userid#" />
<cfhttpparam name="per_page" type="url" value="15" />
</cfhttp>
<!---parse the XML doc--->
<cfset application.FlickrXml = XmlParse(CFHTTP.FileContent) />
</cfif>
<!---Is XML request returned properly?--->
<cfif LCase(application.FlickrXml.XmlRoot.XmlAttributes['stat']) EQ "ok">
<cfelse><cfoutput>Failed</cfoutput><cfabort>
</cfif>
<!---Set the time this was done--->
<cfset application.FlickrXmlstarted=Now()>
<!---Parse/narrow down the info I need into an Array--->
<cfset photosets=XMLSearch(application.FlickrXml, '/rsp/photosets/photoset')>
</cfsilent>
Part Two: Output: I've got my XML data, and now just need to loop over the Array.
<h2>Photos</h2>
<!--XML last cached #application.FlickrXmlstarted#--->
<cfloop from="1" to="#ArrayLen(photosets)#" index="i">
<cfoutput>
<div id="photoset" class="clear">
<!---Output primary image--->
<img class="left" src="http://farm#photosets[i].XmlAttributes['farm']#.static.flickr.com/#photosets[i].XmlAttributes['server']#/#photosets[i].XmlAttributes['primary']#_#photosets[i].XmlAttributes['secret']#_s.jpg">
<!---output title--->
<h4><a href="http://www.flickr.com/photos/#userid#/sets/#photosets[i].XmlAttributes['id']#/">#photosets[i].title.xmltext#</a></h4>
<!---If description exists, show--->
<cfif len(photosets[i].description.xmltext)>
<p>#photosets[i].description.xmltext#</p>
</cfif>
</div>
</cfoutput>
</cfloop>
</cfoutput>
And that's it: I've cached the XML data in the Application scope for speed, and I'm looping over the XML data object. If a description exists for the set, it's output.
There are no comments for this entry.
[Add Comment]