« AJAX Agent, Easy To Use Ajax PHP Framework | Main | New AJAX plugin for jQuery »

Explore Amazon Web Services with AJAX - Price Compare - Day 2

Day 2 of Explore Amazon Web Services with AJAX, yesterday we talked about a very basic usage which was the LiveSearch. Today I'm trying to explore more features of AWS, the idea is to compare prices from the different Amazon websites which are US, UK, FR, DE, JP and CA. I find it interesting to have a better idea of prices in the different websites or to simply buy the product from the nearest service to you.

http://ajax.phpmagazine.net/upload/2006/02/amazon-compare-price-thumb.png

Technically you are going to notice that there isn't a big change in code, we are still using nusoap and prototype. But this time we are going to make request from different servers to retrieve price informations. There is two approach to do this, the first one is to request the six servers in the same time and retrieve all the data, and the second is to request server by server and display result progressively. Today I'm going to explain the first approach, and keep the second one for you to give it a try if you are interested.

First we are going to start with the PHP script, and this time are going to request the remote method ItemLookup, using the ASIN as a parameter.


<?php

/**
* AWS 4.0 with AJAX demo 2 using nusoap and prototype
* Amazon Price Comparaison
*
* Author Hatem Ben Yacoub <hatem@php.net>
* http://ajax.phpmagazine.net/
*/

// include nusoap classes

require_once("lib/nusoap.php");
require_once(
"lib/class.wsdlcache.php");

//------------------------------------------------//
//
//    Enter your AWS subscription key here
//
//------------------------------------------------//

$SubscriptionId = '';

//------------------------------------------------//

$AmazonUS = 'http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl';
$AmazonUK = 'http://webservices.amazon.com/AWSECommerceService/UK/AWSECommerceService.wsdl';
$AmazonDE = 'http://webservices.amazon.com/AWSECommerceService/DE/AWSECommerceService.wsdl';
$AmazonJP = 'http://webservices.amazon.com/AWSECommerceService/JP/AWSECommerceService.wsdl';
$AmazonFR = 'http://webservices.amazon.com/AWSECommerceService/FR/AWSECommerceService.wsdl';
$AmazonCA = 'http://webservices.amazon.com/AWSECommerceService/CA/AWSECommerceService.wsdl';


function
GetComparePriceParams($ItemId) {
    global
$SubscriptionId;

    
$request = array(
        
"ItemId" => $ItemId,
        
"ResponseGroup" => "OfferSummary"
    
);

    
$itemSearch = array(
        
'SubscriptionId' => $SubscriptionId,
        
'AssociateTag' => 'phpmagazine-20',
        
'Request' => $request
    
);

    return
$itemSearch;
}

$countries = array('US','UK','FR','CA','JP','DE');

        
$display = "<table width=100%>

<tr align=center>
    <th>Vendor</th>
    <th>New Price</th>
    <th>Used Price</th>
    <th>Collectibe Price</th>
    <th>New</th>
    <th>Used</th>
    <th>Collectible</th>
    <th>Refurbished</th>
</tr>\n"
;

foreach(
$countries as $v) {

        eval(
"\$wsdlurl = \$Amazon$v;");
        
// Try getting data from cache
        
$cache = new wsdlcache('cache', 0);
        
$wsdl = $cache->get($wsdlurl);
        if (
is_null($wsdl)) {
            
$wsdl = new wsdl($wsdlurl);
            
$cache->put($wsdl);
        } else {
            
$wsdl->debug_str = '';
            
$wsdl->debug('Retrieved from cache');
        }
        
        
$client = new soapclient($wsdl,true);
        
$result = $client->call('ItemLookup', array('body' => GetComparePriceParams($_GET['ItemId'])));
        
$item = $result['Items']['Item'];
        
$display .= "
<tr>
<td><img src=\"flags/$v.png\" border=0> Amazon $v</td>
<td>"
.$item['OfferSummary']['LowestNewPrice']['FormattedPrice']."</td>
<td>"
.$item['OfferSummary']['LowestUsedPrice']['FormattedPrice']."</td>
<td>"
.$item['OfferSummary']['LowestCollectiblePrice']['FormattedPrice']."</td>
<td>"
.$item['OfferSummary']['TotalNew']."</td>
<td>"
.$item['OfferSummary']['TotalUsed']."</td>
<td>"
.$item['OfferSummary']['TotalCollectible']."</td>
<td>"
.$item['OfferSummary']['TotalRefurbished']."</td>
</tr>\n\n"
;

    
}
$display .= "</table>";

echo
$display;
exit;

?>


Now we need to make change in the previous PHP script to include link "Compare Prices" and a div where the result will be displayed. I also included a spinner in front of every link, and used the ASIN as id to use it in my javascript. You can use the script below and replace it in the LiveSearch script


<?php

$display
.= "<div class=item>
<h2><a href=\""
.$item['DetailPageURL']."\">".$item['ItemAttributes']['Title']."</a></h2>
<a href=\""
.$item['DetailPageURL']."\"><img src=\"".$item['SmallImage']['URL']."\" width=\"".$item['SmallImage']['Width']['!']."\" height=\"".$item['SmallImage']['Height']['!']."\" align=\"right\" border=0></a>
<ul>
    <li><strong>ISBN :</strong> "
.$item['ItemAttributes']['ISBN']."  </li>
    <li><strong>Author(s) :</strong> "
.$author."</li>
    <li><strong>Publisher :</strong> "
.$item['ItemAttributes']['Publisher']."</li>
    <li><strong>Price: </strong> <font color=red style='text-decoration: line-through;'>"
.$item['ItemAttributes']['ListPrice']['FormattedPrice']."</stroke></font> <font size=+2>".$item['OfferSummary']['LowestNewPrice']['FormattedPrice']."</font>
    <a href=\"#\" onclick=\"compareprice('"
.$item['ASIN']."')\"><strong>Compare Prices </strong></a> <img alt=\"Spinner\" id=\"price_spinner_".$item['ASIN']."\" src=\"spinner.gif\" style=\"display:none;\" />
    </li>
</ul>
<div id=\""
.$item['ASIN']."\"></div>
</div><br/>\n"
;

?>


The Javascript in this case is very simple, you have just to add the new function ComparePrice() which will request data from the php script and update the page.


    function compareprice(ItemId){
          var url = 'demo2.php';
        var pars = 'ItemId='+ItemId;
        var target = ItemId;
        
        var myAjax = new Ajax.Updater(
            target,
            url,
            {
                method: 'get',
                parameters: pars,
                onComplete:function(request){Element.hide('price_spinner_'+ItemId)},
                onLoading:function(request){Element.show('price_spinner_'+ItemId)},
                onFailure: reportError
            });
    }

That's all ! Now you can see the demo working online of the AJAX Amazon Price Comparaison, and see the availability of your favorite book, the different prices ... etc.

Conclusion

This is another way to use AJAX with Amazon Web Services, the idea is interesting since we are making 6 requests from AWS everytime we want to compare prices. AJAX make it more easy and more useful. There is another approach that you can try it yourself, which is to request comparaison data country by country and display it progressively, and then we can see the difference not in performance but in the usability of the application.

Update : Download code of this demo

Bookmark this article at these sites
Comments
1

Wow... that is an interesting concept. Is Amazon really getting into the price comparison business?

Amazon is kind of twisting the e-commerce market quite a bit, I mean isn't it contradicting to sell and compete against yourself? Hmm...

Andrew
http://www.DataFeedFile.com

2

Would it be possibile to load an XML file instaid of AWSECommerceService.wsdl? I have some data in XML and I would like to make this script work for my site.

Thanx,
Mircea.

3

@Mircea if you have access to the old XML service you can use it, but there will be no need to nusoap. Simple XML can do everything for you.

Post a comment





(Email will remain hidden)





Please enter the security code you see here




Related entries
Email to a friend
Email this article to:


Your email address:


Message (optional):