6CS028 Workshop - Web Services

From mi-linux
Revision as of 14:46, 28 July 2009 by In9352 (talk | contribs)
Jump to navigationJump to search

Main Page >> Web Application Development >> Workbook >> Week 07

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: