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

From mi-linux
Jump to navigationJump to search
Line 1: Line 1:
 
[[Main Page]] >> [[CP3207|Web Application Development]] >> [[Web Application Developpment - Workbook|Workbook]] >> Week 07
 
[[Main Page]] >> [[CP3207|Web Application Development]] >> [[Web Application Developpment - Workbook|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 Amazon API
 +
* SOAP – The Google Search API
  
 
== A REST example : Amazon ==
 
== A REST example : Amazon ==

Revision as of 14:57, 28 July 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 Amazon API
  • SOAP – The Google Search API

A REST example : Amazon

<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);
 
  // Browse object and display book titles
  foreach($xml->BrowseNodes->BrowseNode->TopSellers->TopSeller as $book)
  {
    echo $book->Title."<br>";
  }

?>

And:

<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>";
    
  }
?>

A SOAP example

First, you need to download the NuSOAP library... it's free!

Then:

<?
	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>";
  }
?>