Home arrow HTML arrow Page 3 - Hunting the Web Services at Amazon
HTML

Hunting the Web Services at Amazon


Why is Amazon so popular? How can you use Amazon's web services? Ahm answers these questions and will help you develop websites using this service.

Author Info:
By: Ahm Asaduzzaman
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
March 17, 2003
TABLE OF CONTENTS:
  1. · Hunting the Web Services at Amazon
  2. · Writing Your Application
  3. · Walkthrough Example
  4. · Conclusion

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Hunting the Web Services at Amazon - Walkthrough Example
(Page 3 of 4 )

Now we will walk through an example, which demonstrates, how we can develop software with PHP that consume Web Services from Amazon.com. 

<html>
<head>
<basefont face="Verdana" size="2">
</head>
<body bgcolor="#F0F0F0">
<table width="100%" cellspacing="1" cellpadding="5">
<tr>
 <td bgcolor="Navy"><font color="white" size="-1"><b>Hunting at Amazon</b></font></td>
</tr>
</table>
<?
if (!$_POST['query'])
{
?>
Select a category:
<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<select name="type" size="5">
 <option value="author">Author</option>
    <option value="artist">Artist</option>
    <option value="actor">Actor</option>
    <option selected value="director">Director</option>
    <option value="manufacturer">Manufacturer</option>
</select>
<BR><BR> Enter a search term:<BR><input type="text" name="query">
</form>
<?
}
else
{
$ServiceType = $_POST['type'];
// include class
include("nusoap.php");
// create a instance of the SOAP client object
$soapclient = new soapclient("
http://soap.amazon.com/schemas2/AmazonWebServices.wsdl ", true);
// Create a proxy so that WSDL methods can be accessed directly. We can now invoke the remote method as method of the proxy class, and pass our parameters directly. The proxy objects will handle the details such as matching up parameters values to their names, assigning namespaces and types.
$proxy = $soapclient->getProxy();
// at this point you can use the following commented lines of code as check for error.
/*
if ($err = $proxy->getError()) {
print "ERROR: $err";
} */
// set up an array containing input parameters to be
// passed to the remote procedure
// use a switch loop to define the search parameters for each type
switch ($ServiceType)
{
 case "author":
  $mode = "books";
  $MyFunc = "AuthorSearchRequest";
  break;
 case "artist":
  $mode = "music";
  $MyFunc = "ArtistSearchRequest";
  break;
 case "actor":
  $mode = "vhs";
  $MyFunc = "ActorSearchRequest";
  break;
 case "director":
  $mode = "vhs";
  $MyFunc = "DirectorSearchRequest";
  break;
 case "manufacturer":
  $mode = "electronics";
  $MyFunc = "ManufacturerSearchRequest";
  break;
}
//htmlentities - Convert all applicable characters to HTML entities
$params = array(
        $ServiceType         => htmlentities($_POST['query']),   
      'page'        => 1,
      'mode'        => $mode,
      'tag'         => 'Webservice-20',
      'type'        => 'lite',
      'devtag'      => 'D39A7USMOTRSNR' // the token I got from Amazon
);
// invoke the method
$MyResult = $proxy->$MyFunc($params);
/* if you are interested to see the contents of SOAP envelop  that has exchanged, uncomment the following two lines of codes.*/
//echo '<xmp>'.$proxy->request.'</xmp>';
//echo '<xmp>'.$proxy->response.'</xmp>';
// uncomment following lines to view the result
//print_r($MyResult);
$MyTotal = $MyResult['TotalResults'];
$MyItems = $MyResult['Details'];
// format and display the results
?>
Search for <b><? echo $_POST['query']; ?></b> returned <? echo $MyTotal; ?> matches from Amazon.
<p>
<table width="100%" border="0" cellspacing="5" cellpadding="2">
<?
// parse the $MyItems[] array and extract the necessary information to display
foreach ($MyItems as $i)
{
?>
<tr>
<td align="center" valign="top" rowspan="3"><a href="<? echo $i['Url']; ?>"><img border="1" src=<? echo $i['ImageUrlSmall']; ?>></a></td>
<td><font size="-1"><b><? echo $i['ProductName']; ?></b> /
<?
if (is_array($i['Authors']))
{
 //Join array elements with a string
 echo implode(", ", $i['Authors']);
}
else if (is_array($i['Artists'])) //Finds whether a variable is an array
{
 echo implode(", ", $i['Artists']);
}
else if ($i['Manufacturer'])
{
 echo $i['Manufacturer'];
}?>
</font></td>
</tr>
<tr>
<td align="left" valign="top"><font size="-1">List Price: <? echo $i['ListPrice']; ?> / Our Price: <? echo $i['OurPrice']; ?></font></td>
</tr>
<tr>
<td align="left" valign="top"><font size="-1"><a href="<? echo $i['Url']; ?>">Read more at Amazon</a></font></td>
</tr>
<tr>
<td colspan=2> </td>
</tr>
<?
}
?>
</table>
<?
}
?>
</body>
</html>


blog comments powered by Disqus
HTML ARTICLES

- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5
- BBC Adopts HTML 5, Mozilla Addresses Issues
- Advanced Sticky Footers in HTML and CSS
- HTML and CSS Sticky Footers
- Strategy Analytics Predicts HTML5 Phones to ...
- HTML5 Guidelines for Web Developers
- Learning HTML5 Game Programming
- More Engaging CSS3 and HTML Background Effec...
- Engaging HTML and CSS3 Background Effects
- More Web Columns with CSS3 and HTML
- Columns with CSS3 and HTML
- Creating Inline-Block HTML Elements with CSS
- Drag and Drop in HTML5: Parsing Local Files

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials