« Rasmus's Approach To Building Rich Web Applications | Main | IBM AJAX Toolkit Framework »

Explore Amazon Web Services with AJAX - Node Browser - Day 3

So Day 3 for Exploring Amazon Web Services with AJAX, we are going to see another feature in AWS today which is the Node Browser. The feature is also based on ItemSearch, but this time by setting a node number and a SearchIndex. In addition to Paging which I have written in server-side, so you can browse all products available in a selected categories.

The idea behind this Node Browser is very simple, and it's a feature that is very useful for example to setup a website for selling Amazon products. You can choose a list of categories that you want to display, and setup an AJAX-based interface to show products in the different available pages.

http://ajax.phpmagazine.net/upload/2006/03/ajax-node-browser-thumb.png

You can find a complete list of available nodes in the Amazon E-Commerce Service Developer Guide, available for the different countries too. So the PHP code this time will be separated into two parts the Pager and the SOAP request. In the beginning I was thinking about making an AJAX Pager, but I find it more easy to make it server side. Anyway if you are interested Jigar Desai have an excellent AJAX Pager control based on Prototype.


<?php

/**
* AWS 4.0 with AJAX demos using nusoap and prototype
*
* 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';

function
GetNodeSearchParams($Node,$Mode,$Page = 1) {
    global
$SubscriptionId;

    
$request = array(
        
"SearchIndex" => $Mode,    
        
"ItemPage" => $Page,    
        
"BrowseNode" => $Node,    
        
"ResponseGroup" => "Medium"
    
);

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

    return
$itemSearch;
}


// Setting the WSDL
$wsdlurl = $AmazonUS;

// Try getting data from cache
$cache = new wsdlcache('cache', 120);
$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);
$client->soap_defencoding = 'UTF-8';


$node = (integer)$_GET['Node'];
$mode = $_GET['Mode'];
$page = (isset($_GET['Page']))?(integer)$_GET['Page']:1;

$result = $client->call('ItemSearch', array('body' => GetNodeSearchParams($_GET['Node'],$_GET['Mode'],$_GET['Page'])));
$TotalResults = $result['Items']['TotalResults'];

if (
$TotalResults > 0) {

    
$totalPage = $result['Items']['TotalPages'];
    
    if (
$page <= 10) {
        
$start = 1;
        
$until = 10;
    } else {
        
$start = $page -5;
        
$until = $page +5;
    }
    
    
$Pager = '';
    if (
$page >1) {
        
$Pager .= "<a  href=\"#\" onclick=\"Browse($node,'$mode',".($page+-1).")\"><</a>&nbsp;\n";
        
$Pager .= "<a  href=\"#\" onclick=\"Browse($node,'$mode',1)\"><<</a>&nbsp;\n";
    }
    for(
$i = $start;$i <= $until; $i++) {
        if (
$i != $page) {
            
$Pager .= "&nbsp;<a href=\"#\" onclick=\"Browse($node,'$mode',$i)\">[$i]</a>&nbsp;\n";
        } else  {
            
$Pager .= "&nbsp;[$page]&nbsp;\n";
        }
            
    }
    
$Pager .= "&nbsp;<a href=\"#\"  onclick=\"Browse($node,'$mode',".($page+1).")\">></a>&nbsp;<a href=\"#\"  onclick=\"Browse($node,'$mode',".$totalPage.")\">>></a>\n";


    
$display = "<div class=item>".$Pager."</div><br/>";
    foreach(
$result['Items']['Item'] as $item) {
        
        
$author = (is_array($item['ItemAttributes']['Author']))?@implode(', ',$item['ItemAttributes']['Author']):$item['ItemAttributes']['Author'];
        
        
$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"
;
    
    }
    
$diaplay .= "<div class=item>".$Pager."</div>\n";
    
} else {
    
$display = "<font color=red><strong>Invalid node $node</strong></font>";
}

echo
$display;
exit;



?>


I have also kept the same display used in the first and second demo, so we can have all the features mixed together in the same interface. The Javascript is very easy, almost the same thing, but this time we have Node, Mode and Pag as parameters to send. The result is retrieved and displayed in the result div.


    function Browse(Node,Mode,Page){
          var url = 'demo3.php';
        var pars = 'Node='+Node+'&Mode='+Mode+'&operation=BrowseNode&Page='+Page;
        var target = 'result';

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

I have updated the AJAX AMAZON demo page so you can see the different features working online and browsing some Amazon categories such Books, Music, Software, Computers, video Games, DVDs, Office Products. You can download the source code used in this demo and the previous ones.

Conclusion

Today feature is something very basic in the conception of an online store based on Amazon products. You can personalized as you want to display products, paging ... etc. and we are going to see more features in the next days, so stay tuned. If there is something that you want to see drop us a message before the Explore Amazon Web Services with AJAX week will be over.

Bookmark this article at these sites
Comments
1

Looks great, I'll try it on my site. Thanks

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):