6CS028 Workshop - Web Services

From mi-linux
Revision as of 13:26, 4 March 2014 by In9352 (talk | contribs)
Jump to navigationJump to search

Main Page >> Advanced Web Technologies >> Workbook >> Week 07

There are so many exciting APIs available on the web that it is impossible to cover them all here… so let’s have a look at a known REST example and a known SOAP example:

  • REST - The Wikipedia API
  • SOAP - The Flickr Search API
  • The Amazon API

REST - The Wikipedia API

All features shown on this page should be working, but the API is still in active development, and may change at any time. Make sure to monitor their mailing list for any updates.

Let’s simply find all all the Wikipedia pages that link to my website www.bdtheque.com :o)

With REST, it's all about calling the right URL, passing in the right parameters... here is the URL we need:

Click me!

Note that we are passing the following parameters:

  • action = query
  • list = exturlusage (the service we are requesting)
  • format = xml (the format of the response)
  • euquery = www.bdtheque.com (the parameter required for this service)

If you click on the URL, your browser simply display the XML data (try looking at the page's source code).

So our PHP code looks very much like the code we used to access an RSS feed. Indeed in both cases we are accessing a remote XML file!

<?
  
  // Since 2010 a user agent is required
  ini_set('user_agent', 'University of Wolverhampton');

  $url = "http://en.wikipedia.org/w/api.php?"
        ."action=query&"
        ."list=exturlusage&"
        ."format=xml&"
        ."euquery=www.bdtheque.com";
          
  // Get raw data
  $response = file_get_contents($url);
  
  // Convert raw data to object
  $xml = simplexml_load_string($response);
 
  // Loop through data and display
  foreach($xml->query->exturlusage->eu as $link)
  {
    echo "<H1>{$link['title']}</H1><P>Links to: {$link['url']}</P>";
  }          

?>

And that’s it! Your page should now be displaying all the Wikipedia pages that link to my website www.bdtheque.com.

Here's a working example on my account.

A SOAP example: Flickr Search

Accessing SOAP APIs is just as simple. But first you need to download the NuSOAP library. It will do a lot of the work for you.

Simply unzip the archive in your web pages’ folder.

Now the code… well nothing too difficult here:

  • First you include the library you have just downloaded
  • Then you create a SOAP client object and set its encoding
  • Then you prepare your SOAP request and send it.
  • The response is a multidimensional array. You can use the print_r command to display it fully if you wish.

The Flickr SOAP API documentation is here:

<?
  require("nusoap/lib/nusoap.php");

  // Create soap client
	$client=new nusoap_client("http://api.flickr.com/services/soap/");

  // Set encoding
	$client->soap_defencoding = 'UTF-8';

  // Prepare request
	$request=array(
		'api_key'=>'489ef07aabc7de443750a542058090bf',
    'method'=>'flickr.photos.search',
		'tags'=>'Radiohead',
		'per_page'=>'30'
	);

  //  Send request and read response
	$result = $client->call('FlickrRequest', $request, "urn:flickr");
  $data = html_entity_decode($client->responseData);

  // get rid of name spaces
  $data = str_replace("s:", "", $data);
  $data = str_replace("x:", "", $data);

  // Convert xml to object
  $xml = simplexml_load_string($data);
  
  foreach($xml->Body->FlickrResponse->photos->photo as $one_photo)
  {
    $url = "http://farm{$one_photo['farm']}.static.flickr.com/{$one_photo['server']}/{$one_photo['id']}_{$one_photo['secret']}.jpg";
    $title = $one_photo['title'];
    
    //print "<h2>{$title}<h2>";
    print "<img src=\"{$url}\" title=\"$title\" style=\"height: 100px; padding: 2px\">";
  }
?>

All done! Your page should look like this: