Recent Changes - Search:

Network Programming

This website demonstrates using wikis as teaching and learning tool.

The course instructor is happy to share the teaching materials here with those who find it readable.

Access Flickr API with PHP

Overview: You will create a simple PHP scripts to search the flickr site for some photos. To get the data your use the flickr API to run a simple search and return the results in a serialized array.


Activity One

  • Study the following script. Answer the question that follows.
      <?php
      function search($query = null) { 
        $apiKey = '24d52be90f046c1d2f05bbd2f47d4xxx';
        $search = 'http://flickr.com/services/rest/?method=flickr.photos.search&api_key=' . $apiKey . '&text=' . urlencode($query) . '&per_page=10&format=php_serial';
        $result = file_get_contents($search);
        $result = unserialize($result);
        return $result;
      }

      $data = search('causeway bay'); 
      foreach($data['photos']['photo'] as $photo) { 
        // the image URL becomes somthing like 
        // http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}.jpg
        echo '<img src="http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg">';
      }
      ?>

Some notes about the script:

The script uses the php function file_get_contents to receive data from flickr.
The data received will be a serialized PHP array which means all we need to do is unserialize the array and we will easily be able to use the data returned.
As an alternative we can use a cURL function to get the data, for example if the function "file_get_contents" is not allowed on your web host.

Questions:

1) What type of Web Services standards (REST or SOAP) that the script accesses? Explain your answer.
2) What do you need if you want to run the above programs in your own machine?
3) Can you use the functions provided to search photos within a given geographical location? If so, explain how. If not, explain why?
4) Can you use a Java program to implement the same functions? Explain your answer.

Activity Two

  • Test the script with your own server and API key.
The API methods we use does not require any authentication, but we need to use an API key.
Get your API key on the flickr web application site. You need an Yahoo account to login.
  • Test the script with different queries.

Other similar resources:

phpFlickr is a class written by Dan Coulter in PHP (compatible with PHP4 and PHP5) to act as a wrapper for Flickr’s API. Methods process the response and return a friendly array of data to make development simple and intuitive.

Submission

  • Please submit your work to Steven by email.
  • Email: sochoy@ouhk.edu.hk
  • Email title: NP Tutorial 16 Submission -SXXXXXXX

Edit - History - Print - Recent Changes - Search
Page last modified on February 08, 2010, at 02:42 PM