Difference between revisions of "6CS028 Workshop - Web Services"

From mi-linux
Jump to navigationJump to search
Line 3: Line 3:
 
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:
 
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 Amazon API
+
* REST - The Wikipedia API
 
* SOAP - The Google Search API
 
* SOAP - The Google Search API
 +
* A challenge for you!
  
== A REST example: Amazon ==
+
== REST - The Wikipedia API ==
  
Note that Amazon is one of the rare providers to still offer both REST and SOAP interfaces to their services… let’s have a look at their REST 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 our mailing list for any updates.
  
First, let’s simply select the top selling books on Amazon.com
+
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 for the top selling books on Amazon.com:
+
With REST, it's all about calling the right URL, passing in the right parameters... here is the URL we need:
  
[http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=0NR4EHP6HAVW61V6P1G2&Operation=BrowseNodeLookup&BrowseNodeId=283155&ResponseGroup=TopSellers Click me!]
+
[http://en.wikipedia.org/w/api.php?action=query&list=exturlusage&format=xml&euquery=www.bdtheque.com Click me!]
  
 
If you click on the URL, your browser simply display the XML data (try looking at the page's source code).
 
If you click on the URL, your browser simply display the XML data (try looking at the page's source code).
Line 21: Line 22:
  
 
<pre>
 
<pre>
<H1>Top Selling Books on Amazon.com at <?= date("d-m-Y H:i"); ?></H1>
 
 
<?
 
<?
 
  // My key is 0NR4EHP6HAVW61V6P1G2
 
  // APPLY FOR YOUR OWN KEY!!
 
 
  // Node for books
 
  $node = 283155;
 
 
  // Build REST url
 
  $url = "http://webservices.amazon.com/onca/xml?";
 
  $url.= "Service=AWSECommerceService&";
 
  $url.= "SubscriptionId=0NR4EHP6HAVW61V6P1G2&";
 
  $url.= "Operation=BrowseNodeLookup&";
 
  $url.= "BrowseNodeId=$node&";
 
  $url.= "ResponseGroup=TopSellers";
 
  
 +
  $url = "http://en.wikipedia.org/w/api.php?"
 +
        ."action=query&"
 +
        ."list=exturlusage&"
 +
        ."format=xml&"
 +
        ."euquery=www.bdtheque.com";
 +
         
 
   // Get raw data
 
   // Get raw data
 
   $response = file_get_contents($url);
 
   $response = file_get_contents($url);
Line 44: Line 36:
 
   $xml = simplexml_load_string($response);
 
   $xml = simplexml_load_string($response);
 
   
 
   
   // Browse object and display book titles
+
   // Loop through data and display
   foreach($xml->BrowseNodes->BrowseNode->TopSellers->TopSeller as $book)
+
   foreach($xml->query->exturlusage->eu as $link)
 
   {
 
   {
     echo $book->Title."<br>";
+
     echo "<H1>{$link['title']}</H1><P>Links to: {$link['url']}</P>";
   }
+
   }
  
 
?>
 
?>
 
</pre>
 
</pre>
  
And that’s it! Your page should now be displaying the titles of the top selling books.
+
And that’s it! Your page should now be displaying all the Wikipedia pages that link to my website www.bdtheque.com.
 
 
But what if you want to know more about those books? Well you need to send another request to ask for more details on each book… The following example builds on the previous one, and displays a picture of the book cover next to its title! You could display a lot more (authors, similar books, prices etc…) but we’ll leave that bit up to you.
 
 
 
<pre>
 
<H1>Top Selling Books on Amazon.com at <?= date("d-m-Y H:i"); ?></H1>
 
<?
 
 
  // My key is 0NR4EHP6HAVW61V6P1G2
 
  // APPLY FOR YOUR OWN KEY!!
 
 
  // Node for books
 
  $node = 283155;
 
 
  // Build REST url
 
  $url = "http://webservices.amazon.com/onca/xml?";
 
  $url.= "Service=AWSECommerceService&";
 
  $url.= "SubscriptionId=0NR4EHP6HAVW61V6P1G2&";
 
  $url.= "Operation=BrowseNodeLookup&";
 
  $url.= "BrowseNodeId=$node&";
 
  $url.= "ResponseGroup=TopSellers";
 
       
 
  // Get raw data
 
  $response = file_get_contents($url);
 
 
 
  // Convert raw data to object
 
  $xml = simplexml_load_string($response);
 
 
  // Put a;; asin numbers into a variable
 
  $ASIN = "";
 
  foreach($xml->BrowseNodes->BrowseNode->TopSellers->TopSeller as $book)
 
  {
 
    $ASIN = $ASIN.$book->ASIN.",";
 
  }
 
 
  // Make second call, asking for more details
 
 
 
  $url = "http://webservices.amazon.com/onca/xml?";
 
  $url.= "Service=AWSECommerceService&";
 
  $url.= "SubscriptionId=0NR4EHP6HAVW61V6P1G2&";
 
  $url.= "Operation=ItemLookup&";
 
  $url.= "ItemId=$ASIN&";
 
  $url.= "ResponseGroup=Medium";
 
 
 
  $response = file_get_contents($url);
 
  $xml = simplexml_load_string($response);
 
 
  foreach($xml->Items->Item as $bd)
 
  {
 
    $ASIN = $bd->ASIN;
 
    $image = $bd->MediumImage->URL;
 
    $url = $bd->DetailPageURL;
 
    $Title =$bd->ItemAttributes->Title;
 
 
    echo "<IMG src=\"$image\">";
 
    echo "$Title<BR>";
 
   
 
  }
 
?>
 
</pre>
 
  
 
== A SOAP example: Google Search ==
 
== A SOAP example: Google Search ==

Revision as of 16:09, 14 October 2009

Main Page >> Web Application Development >> 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 Google Search API
  • A challenge for you!

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 our 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!

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!

<?

  $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.

A SOAP example: Google 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.
<?
  require("nusoap/lib/nusoap.php");

  // What we are searching for
  $search = "Alix Bergeret";

  // Create soap client
  $client = new soapclient("http://api.google.com/GoogleSearch.wsdl", true);

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

  // Prepare request
  $request = array(
		'key'=>'4B0KufpQFHJxhAxzua0tR11ElLNrHRJ6',
		'q'=>$search,
		'start'=>0,
		'maxResults'=>5,
		'filter'=>false,
		'restrict'=>'',
		'safeSearch'=>true,
		'lr'=>'',
		'ie'=>'',
		'oe'=>'',
	);

  // Send request and read response
  $result = $client->call('doGoogleSearch', $request, "urn:GoogleSearch", "urn:GoogleSearch");

  // Loop through array and display
  foreach($result['resultElements'] as $one_result)
  {
    print "<h1>{$one_result['title']}</h1>";
    print "<p>{$one_result['snippet']}</p>";
  }
?>

All done!