O'Reilly logo

Flickr Hacks by Jim Bumgardner, Paul Bausch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #29. Surf Your Contacts

Find out what your contacts are up to outside of Flickr.

Most of your Flickr contacts really do have lives outside of Flickr, and you can often find a link to a friend's other life on his Flickr profile page. When you create a Flickr profile, you have the option to enter your web site URL and web site name, which will show up in your profile just above your location. Most Flickr members use this space to point to their home pages or weblogs.

If you don't already regularly visit your Flickr contacts' external sites, you might learn something more about your acquaintances and the photographers you admire by taking a look. If you have just a handful of contacts, you could visit each of your contacts' profile pages one by one and make a list of URLs you'd like to visit; but if you have more than a dozen contacts, a bit of PHP scripting can help you put together a master surfing list in a few minutes.

This hack compiles a list of your contacts, assembles their profile page URLs, and visits each one, looking for a web site. You'll need a free Flickr API key and your Flickr NSID, both of which can be found at the Flickr API documentation page (http://www.flickr.com/services/api/). This hack also relies on the simple XML tools built into PHP 5, which make working with Flickr API responses a snap.

The Code

Copy the following code to a file called flickr_surf.php, and be sure to put in your own Flickr API key and Flickr NSID:

// flickr_surf.php
// Finds web sites of Flickr contacts for given
// Flickr NSIDs. Use it to gather your Flickr 
// contacts' web sites onto one page for easy 
// surfing. 
// You can get an API key and read the full documentation
// for the Flickr API at http://www.flickr.com/services/api/

// Set your Flickr API key and your user NSID
$api_key = "insert your Flickr API key";
$my_nsid = "insert your Flickr NSID";

// This is a greedy script requiring lots of time

// Construct a Flickr query to grab your contacts
$req_url = "http://www.flickr.com/services/rest/";
$req_url .= "?method=flickr.contacts.getPublicList";
$req_url .= "&api_key=$api_key";
$req_url .= "&user_id=$my_nsid";

// Make the request
$flickr_contacts = file_get_contents($req_url);

// Parse the XML
$fc_xml = simplexml_load_string($flickr_contacts);
    <style type="text/css">
    BODY {
<h2>Surf Flickr Friends</h2>
// Loop through the contacts
foreach ($fc_xml->xpath('//contact') as $contact) {
    $nsid = $contact['nsid'];
    $username = $contact['username'];

    // Construct a Flickr query to grab contact info
    $req_url = "http://www.flickr.com/services/rest/";
    $req_url .= "?method=flickr.people.getInfo";
    $req_url .= "&api_key=$api_key";
    $req_url .= "&user_id=$nsid";

    // Make the request
    $contact_info = file_get_contents($req_url);

    // Parse the XML
    $ci_xml = simplexml_load_string($contact_info);

    // Get contact info
    $iconserver = $ci_xml->person[0]['iconserver'];
    $realname = $ci_xml->person[0]->realname;
    $profile_url = $ci_xml->person[0]->profileurl;

    // See if a web site exists on the member's profile page
    // with screen-scraping
    $profile_html = file_get_contents($profile_url);
    $regex = '!<p style="font-size: 14px;">.*?<a ';
    $regex .= 'href="(.*?)"><strong>(.*?)</strong></a>!is';
    if (preg_match($regex,$profile_html, $matches)) {
        $contact_siteurl = $matches[1];
        $contact_sitename = $matches[2];

        // If a web site is found, print out the web site URL
        // and contact info
        print "<li style=\"margin-bottom:10px;\"><a href=\"$contact_siteurl\">";
        print "$contact_sitename</a><br />";
        print "<span style=\"font-size:8pt;color:#666;\">";
        print "by <a href=\"$profile_url\">$username</a> /";
        print " $realname</span></li>\n";
    $matches = "";

    // Sleep for 3 seconds to give the Flickr
    // servers a break

You'll notice that the script contacts the Flickr API to find your list of contacts, and then contacts the API again for each contact to find the user's real name and profile URL. Flickr doesn't make site information available via the API, so the script relies on screen-scraping the profile pages to find that data. Screen-scraping involves downloading the HTML of the page and picking through it with regular expressions to cherry-pick the required information. It's a notoriously brittle process, and if Flickr changes the HTML of the profile page, the script won't be able to find the information you want. Be aware that you may need to tweak the regular expression set as $regex to find the web site name and URL.

Note also that the script is fairly request-intensive. For every person in your contact list, this script will make two requests of the Flickr servers. To give the Flickr servers a bit of a break, the script then rests for three seconds by calling sleep(3). This will give the Flickr servers a small rest between each contact, but it also means you'll need to be patient while the script is compiling your list.

Running the Hack

To run the code, upload the script to your web server and browse to the page. The URL should look something like this:


Because of the numerous HTTP requests and the built-in delay, expect your script to take several minutes to execute. And, of course, the more contacts you have, the more time the script will need to compile the list. When it's finished, you should see a list of web sites from your Flickr contacts like the one in Figure 4-13.

A list of the author's Flickr contacts' web sites

Figure 4-13. A list of the author's Flickr contacts' web sites

If you want to visit the page over and over again, you'll want to save the HTML the script generated rather than rebuilding the list each time you visit the page. To save the HTML, choose File → Save As... (or File → Save Page As...) from your browser's menu. Your contacts' web sites shouldn't change too often, and when you add or remove contacts you can visit flickr_surf.php again to recompile the list and bring it up to date.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required