Load Flickr Pictures using JSONP API

Sam Deering

jsonp-api2

Code to load images from Flickr public gallery images using JSONP API. You can search using tags/keywords or specific users photos to display on your site.

What is JSONP API?

jQuery-JSONP is a compact (1.7kB minified), yet feature-packed, alternative solution to jQuery’s implementation of JSONP.
jQuery-JSONP Features:
* error recovery in case of network failure or ill-formed JSON responses,
* precise control over callback naming and how it is transmitted in the URL,
* multiple requests with the same callback name running concurrently,
* two caching mechanisms (browser-based and page based),
* the possibility to manually abort the request just like any other AJAX request,
* a timeout mechanism.
Official JSONP Site
Download Page

Load Flickr Pictures using JSONP API

This code loads all the publis photos matching the tags:

< !DOCTYPE html>
<html>
<head>
  <style>img{ height: 100px; float: left; }</style>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
<body>
  <div id="images">

</div>
<script>
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
  {
    tags: "jquery",
    tagmode: "any",
    format: "json"
  },
  function(data) {
    $.each(data.items, function(i,item){
      $("<img />").attr("src", item.media.m).appendTo("#images");
      if ( i == 3 ) return false;
    });
  });</script>

</body>
</html>

What a typical Flickr JSON object looks like

({
    "title": "Talk On Travel Pool",
    "link": "http://www.flickr.com/groups/talkontravel/pool/",
    "description": "Travel and vacation photos from around the world, featured on the Talk On Travel blog. Join us in sharing your favourite destinations, your top hot spots, and the places to avoid. ",
    "modified": "2009-02-02T11:10:27Z",
    "generator": "http://www.flickr.com/",
    "items": [
            {
            "title": "View from the hotel",
            "link": "http://www.flickr.com/photos/33112458@N08/3081564649/in/pool-998875@N22",
            "media": {"m":"http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg"},
            "date_taken": "2008-12-04T04:43:03-08:00",
            "description": "<p><a href="http://www.flickr.com/people/33112458@N08/"> Talk On Travel< /a> has added a photo to the pool:< /p> <p><a href="http:// www.flickr.com/photos/33112458@N08/3081564649/" title="View from the hotel"> <img src="http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg" width="240" height="180" alt="View from the hotel" />< /a>< /p> ",
            "published": "2008-12-04T12:43:03Z",
            "author": "nobody@flickr.com (Talk On Travel)",
            "author_id": "33112458@N08",
            "tags": "spain dolphins tenerife canaries lagomera aqualand playadelasamericas junglepark losgigantos loscristines talkontravel"
            }
    ]
})

Great Tutorial on Flickr JSONP API

Win an Annual Membership to Learnable,

SitePoint's Learning Platform