Hi David,
I would like to display 10 products per search query from amazon.com and also want to add functionality so that my visitors can browse the entire set of results returned from amazon for a search query if they so desire. To implement this I need to get the number of products and number of pages returned for a search query. I am doing this
$xml = file_get_contents($url);
MagicParser_parse("string://".$xml,"myAmazonRecordHandler","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");
$items =$xml->Items;
$amazonProduct["results"]["nproducts"]=$items->TotalResults;
$amazonProduct["results"]["npages"]=$items->TotalPages;
but unfortunately the $items turns out not be an array. Do you know how I can do this without creating another function myAmazonRecordHandler2 that takes "xml|ITEMSEARCHRESPONSE/" as an argument.
I thought you might have already implemented such a functionality.
Thanks for the quick solution. I can now get nproducts and npages but I am not sure how to change navigation.php so that previous and next refers to the previous and next pages in the amazon results. Do you have any idea where to start with? Perhaps you might have an implementation from another occasion.
Hi karakartal,
The easiest way is probably to use the $page variable being generated within the page anyway. In your amazon[product].php, look for this code towards the end where the API query is constructed:
$url .= "&Operation=ItemSearch";
if ($page > 1)
{
$url .= "&ItemPage=".$page;
}
Cheers,
David.
--
PriceTapestry.com
Dear David,
should I change the code line in the end of amazon.php
MagicParser_parse($url,"myAmazonRecordHandler","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");
to
function myAmazonMetaDataRecordHandler($record)
{
global $amazonRecord;
$amazonProduct["results"]["nproducts"] = $record["TOTALRESULTS"];
$amazonProduct["results"]["npages"] = $record["TOTALPAGES"];
}
MagicParser_parse("string://".$xml,"myAmazonMetaDataRecordHandler","xml|ITEMSEARCHRESPONSE/");
as above ?
Or there are other settings what should be done to have the similar result ?
Because when I did this simple replace - no results from Amazon came...so I suppose I'm missing something else...
Best regards
Serge
Hello Serge,
There was an error in the original code above; the myAmazonMetaDataRecordHandler function should be;
function myAmazonMetaDataRecordHandler($record)
{
global $amazonRecord;
$amazonRecord["results"]["nproducts"] = $record["TOTALRESULTS"];
$amazonRecord["results"]["npages"] = $record["TOTALPAGES"];
}
(the two lines to set the results array were using $amazonProduct rather than $amazonRecord)
Cheers,
David.
--
PriceTapestry.com
Hello karakartal,
As the Amazon response is limited to 10 items there will hardly be any performance overhead in creating a second record handler function to extract the meta data - I have implemented this on several occasions and it works fine. The XML only has to be retrieved once so there is no delay in having to requery the API at all. Have a go with something like;
function myAmazonMetaDataRecordHandler($record)
{
global $amazonRecord;
$amazonProduct["results"]["nproducts"] = $record["TOTALRESULTS"];
$amazonProduct["results"]["npages"] = $record["TOTALPAGES"];
}
MagicParser_parse("string://".$xml,"myAmazonMetaDataRecordHandler","xml|ITEMSEARCHRESPONSE/");
Cheers,
David.
--
PriceTapestry.com