Hack 89 Program AWS with NuSOAP and PHP


PHP's standard SOAP module NuSOAP makes SOAP simple.

Like Perl, PHP has its emerging standard method of working with SOAP applications. NuSOAP is a single PHP script (with over 4,000 lines of code!) that handles all of the SOAP heavy lifting for you. Get your copy from http://dietrich.ganx4.com/nusoap/ and include the file in the same directory as your scripts.

89.1 The Code

The script, amazon_soap.php, is meant to be run as a web page. It accepts a variable, keyword, in the URL. With this, it creates the proper SOAP request and returns the results as an array.

<title>Amazon Keyword Search</title>
#Use the NuSOAP php library

#Set parameters
$parameters = array('keyword' => $HTTP_GET_VARS['keyword'],
    'type' => 'lite',
    'page' => '1',
    'mode' => 'books',
    'tag' => 'insert associate tag',
    'devtag' => 'insert developer token');

#Create a new SOAP client with Amazon's WSDL
$soapclient = new soapclient('http://soap.amazon.com/schemas2/[RETURN]
$proxy = $soapclient->getproxy(  );

#query Amazon
$results = $proxy->KeywordSearchRequest($parameters);

//echo 'Request: <xmp>'.$proxy->request.'</xmp>';
//echo 'Response: <xmp>'.$proxy->response.'</xmp>';

if (is_array($results['Details'])) {
    print "<p>Search for <b>" . $HTTP_GET_VARS['keyword'] . "</b>" .
          " found " . $results['TotalResults'] . " results." .
          " <br>Here are the first " . count($results['Details']).".".
          " </p><ol>";
    foreach ($results['Details'] as $result) {
        "<li><b>" . $result['ProductName'] . "</b>" .
        "<br /> by " . $result['Authors'][0] .
" <a href='" . $result['Url'] . "'>" . $result['OurPrice']. "</a><br><br>";
    print "</ol>";

#No Results
else {
    print "Your Amazon query for '" . $HTTP_GET_VARS['keyword'] . 
    "' returned no results";

89.2 Running the Hack

To run the code, place the file on a web server, and browse to:


If you run into problems or are curious about what's being sent between the servers, uncomment the //echo statement by removing the slashes. This will print out the entire SOAP request and Amazon's SOAP response. It's a good way to get a sense of the formatting work being done behind the scenes.