You are here:  » Can I Use Magic Parser With Amazon E-Commerce services


Can I Use Magic Parser With Amazon E-Commerce services

Submitted by mmolaire on Sat, 2006-04-15 22:11 in

How can I use Magic Parser with Amazon E-Commerce Services API?

Submitted by support on Sun, 2006-04-16 06:31

Hi There,

Yes - you can use Magic Parser to process the XML responses to Amazon's E-Commerce web services. It is my understanding that in addition to SOAP, you can make a query to the Amazon API using the "REST" method (as an alternative to the SOAP interface), where all the parameters are given in the query string of a URL; so you should be able to specify that as the input file, and give an appropriate format string as the 3rd parameter to MagicParser_parse().

The format string for the ItemSearchResponse is "xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/", but you will be able to construct similar format string values for other types of response from Amazon.

Submitted by mmolaire on Thu, 2006-04-20 05:17

Can you please give me an example. I ma not that savvy with xml
Thanks
Mike Molaire
VerySmartShopping VeryLastMinuteTravelDeals

Submitted by support on Thu, 2006-04-20 08:28

Hi Mike,

I've now posted a new example in the Magic Parser manual for accessing the Amazon Web Services API using the REST method:

Amazon Web Services Product Search in PHP

Cheers,
David.

Submitted by mmolaire on Fri, 2006-04-21 16:50

Thanks David . That helps alot. Looking at the example, it looks like it is not possible to integrate Amazon to the common database for Price Compare. Is that correct?

Thanks again

Mike Molaire


VerySmartShopping.com
        

VeryLastMinuteTravelDeals.com
    

Submitted by support on Fri, 2006-04-21 19:49

Hi Mike,

Amazon have steered clear of offering a product feed so i'm afraid there is no way to import products in bulk into a Price Tapestry site.

Keep an eye on the Price Tapestry forum over the next few weeks, as i'm going to launch a couple of minor updates to the script that will enable new modules to be added that will provide additional product search and lookup tools if there are no results found in your local database. One of those tools will be an Amazon API plugin....

Cheers,
David.

Submitted by crounauer on Mon, 2006-08-07 22:59

Hi David,

I have spruced up your version to the latest of Amazon E-commerce services and integrated it as part of the related items section....

Struggling a bit to use the search query from the original search to query the amazon xml... a pointer would be great - thanks!

<?php
  
require("includes/MagicParser.php");
  
$counter 0;
  function 
myRecordHandler1($amazon)
  {
    global 
$q;
    global 
$counter;
    
$counter++;
    print 
"<div class='related'>";
    print 
"<table width='100%'>";
    print 
"<tr>";
    print 
"<td>";
    print 
"<a href='".$amazon["DETAILPAGEURL"]."'><img border='0' width='80' src='".$amazon["MEDIUMIMAGE/URL"]."' alt='".$amazon["ITEMATTRIBUTES/TITLE"]."' class='picture' /></a>";
    print 
"</td>";
    print 
"<td width='20'>&nbsp;</td>";
    print 
"<td>";
    print 
"<h4><a href='".$amazon["DETAILPAGEURL"]."'>".$amazon["ITEMATTRIBUTES/TITLE"]."</a></h4><br>";
    print 
"".$amazon["CUSTOMERREVIEWS/REVIEW/CONTENT"]."";
    print 
"</td>";
    print 
"<td width='20'>&nbsp;</td>";
    print 
"<td valign='middle' align='center' width='100'>";
    print 
"New:&nbsp;<b>".$amazon["OFFERSUMMARY/LOWESTNEWPRICE/FORMATTEDPRICE"]."</b><br>";
    print 
"Used:&nbsp;<b>".$amazon["OFFERSUMMARY/LOWESTUSEDPRICE/FORMATTEDPRICE"]."</b><br>";
    print 
"<a href='".$amazon["DETAILPAGEURL"]."' class='nobr'>More Information</a>";
    print 
"</td>";
    print 
"</tr>";
    print 
"</table>";
    print 
"</div>";
    return (
$counter == 3);
  }
    
$version "2006-06-28";
    
$operation "ItemSearch";
    
$keyid "xxxx";
    
$tag "xxxx";
    
$responsegroup "Large";
    
$searchindex "Books";
    
$q $_GET["q"];
    
$url "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&Version=".$version."&Operation=".$operation."&AWSAccessKeyId=".$keyid."&AssociateTag=".$tag."&ResponseGroup="$responsegroup."&SearchIndex=".$searchindex."&Keywords=".$q."";
    
MagicParser_parse($url,"myRecordHandler1","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");
?>

Submitted by support on Tue, 2006-08-08 05:57

Hi,

If you are using re-written URLs on your site you would want to use $q directly (as processed earlier in the script) as it will have things like "-" converted back into spaces.

Secondly, when constructing the Amazon URL, you should use $q with the urlencode() function to make sure that the contents are safe to include in a URL, for example:

$url = "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&Version=".$version."&Operation=".$operation."&AWSAccessKeyId=".$keyid."&AssociateTag=".$tag."&ResponseGroup=". $responsegroup."&SearchIndex=".$searchindex."&Keywords=".urlencode($q)."";

Cheers,
David.

Submitted by crounauer on Tue, 2006-08-08 12:08

Hi David,

In the first part you mention to use $q directly, does that mean just using $_GET function when constructing the amazon url?

<?php
".urlencode($_GET["q"])."
?>

Thanks,
Simon.

Submitted by support on Tue, 2006-08-08 15:51

Hi Simon,

$q has been pre-processed by Price Tapestry at the top of search.php. If you use $_GET["q"] it probably won't work with Amazon because the keyword may still contain hyphens instead of spaces; for example, on the page:

http://www.example.com/product/blue-widget.html

$_GET["q"] contains "blue-widget"

whereas

$q contains "blue widget", which is what you need to pass to Amazon.

Cheers,
David.

Submitted by crounauer on Tue, 2006-08-08 20:08

Hi David,

Thanks for your help on this.

I finally fugured out why it wasn't working - really need to work on my debugging techniques!

$q will as you say give "blue widget" as per the above example. This is no good to construct the amazon url as it contains spaces.

Secondly, because I am using this in the related items section, the keywords are often very long and this seems to confuse amazon and hence produces no results.

I created a little script to replace the spaces with "+" and also shorten the search term, while still keeping whole words.

It can be seen in action here Michel Thomas

The first 3 results are from amazon and the following 5 are from the PT database...

<?php
    $version 
"2006-06-28";
    
$operation "ItemSearch";
    
$keyid "xxxx";
    
$tag "xxxx";
    
$responsegroup "Large";
    
$searchindex "Books";
    
$limit 15;
    if( 
strlen($q)>$limit )
    {
    
$q substr$q,0,$limit );
    
$q substr$q,0,-(strlen(strrchr($q,' '))) );
    
$q str_replace(" ""+"$q);
    }
    
$url "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&Version=".$version."&Operation=".$operation."&AWSAccessKeyId=".$keyid."&AssociateTag=".$tag."&ResponseGroup="$responsegroup."&SearchIndex=".$searchindex."&Keywords=".urlencode($q)."";
?>

Submitted by dflsports on Wed, 2006-08-16 18:49

I had the example script working, well, just a copy and paste deal. I really like the posters way of integrating the amazon products into the search results. But I'm struggling with the thread and the code changes.

I was hoping Simon might paste the complate code, just the amazon part

this is what I have, I'm not a php guy, learning and know enough to be dangerous :)

<?php
 
require("MagicParser.php");
  
$counter 0;
  function 
myRecordHandler1($amazon)
  {
    global 
$q;
    global 
$counter;
    
$counter++;
    print 
"<div class='related'>";
    print 
"<table width='100%'>";
    print 
"<tr>";
    print 
"<td>";
    print 
"<a href='".$amazon["DETAILPAGEURL"]."'><img border='0' width='80' src='".$amazon["MEDIUMIMAGE/URL"]."' alt='".$amazon["ITEMATTRIBUTES/TITLE"]."' class='picture' /></a>";
    print 
"</td>";
    print 
"<td width='20'>&nbsp;</td>";
    print 
"<td>";
    print 
"<h4><a href='".$amazon["DETAILPAGEURL"]."'>".$amazon["ITEMATTRIBUTES/TITLE"]."</a></h4><br>";
    print 
"".$amazon["CUSTOMERREVIEWS/REVIEW/CONTENT"]."";
    print 
"</td>";
    print 
"<td width='20'>&nbsp;</td>";
    print 
"<td valign='middle' align='center' width='100'>";
    print 
"New:&nbsp;<b>".$amazon["OFFERSUMMARY/LOWESTNEWPRICE/FORMATTEDPRICE"]."</b><br>";
    print 
"Used:&nbsp;<b>".$amazon["OFFERSUMMARY/LOWESTUSEDPRICE/FORMATTEDPRICE"]."</b><br>";
    print 
"<a href='".$amazon["DETAILPAGEURL"]."' class='nobr'>More Information</a>";
    print 
"</td>";
    print 
"</tr>";
    print 
"</table>";
    print 
"</div>";
    return (
$counter == 3);
  }
    
$version "2006-06-28";
    
$operation "ItemSearch";
    
$keyid "xxxx";
    
$tag "myid";
    
$responsegroup "Large";
    
$searchindex "SportingGoods";
    
$q str_replace(" ""+"$q);
    
$url "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&Version=".$version."&Operation=".$operation."&AWSAccessKeyId=".$keyid."&AssociateTag=".$tag."&ResponseGroup="$responsegroup."&SearchIndex=".$searchindex."&Keywords=".urlencode($q)."";
    
MagicParser_parse($url,"myRecordHandler1","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");
?>

Thanks.

Don

Submitted by crounauer on Wed, 2006-08-16 19:23

Hi Don,

Yes, I will be happy to help where I can, just not sure what it is youre after?

Regards,
Simon.

Submitted by dflsports on Wed, 2006-08-16 20:23

Well I'm after a million dollars but that's a story for another time :)

I'm actually looking to do just abut the same thing your are except my amazon search results would come after the price tapestry search results and I'm looking to use the sporting goods category.

I have spent quite a bit of time trying to get this working and besides the basic example David provided, I'm stuck.

Submitted by support on Wed, 2006-08-16 20:36

Hi Don,

Quick reply for now... I can see what you're trying to do... I think from the code you posted the main problem is how you are beginning and ending your related products HTML - it shouldn't go within myRecordHandler but rather outside it; with myRecordHandler just generating the HTML for each result.

I'll post some more code tomorrow...

Cheers,
David.

Submitted by support on Thu, 2006-08-17 04:48

Hi again;

Firstly, within the code snippet above I would create the DIV header and footer outside of myRecordHandler1, and then just have myRecordHandler1 generate each table row; for example:

<?php
  
require("MagicParser.php");
  
$counter 0;
  function 
myRecordHandler1($amazon)
  {
    global 
$q;
    global 
$counter;
    
$counter++;
    print 
"<tr>";
    print 
"<td>";
    print 
"<a href='".$amazon["DETAILPAGEURL"]."'><img border='0' width='80' src='".$amazon["MEDIUMIMAGE/URL"]."' alt='".$amazon["ITEMATTRIBUTES/TITLE"]."' class='picture' /></a>";
    print 
"</td>";
    print 
"<td width='20'>&nbsp;</td>";
    print 
"<td>";
    print 
"<h4><a href='".$amazon["DETAILPAGEURL"]."'>".$amazon["ITEMATTRIBUTES/TITLE"]."</a></h4><br>";
    print 
"".$amazon["CUSTOMERREVIEWS/REVIEW/CONTENT"]."";
    print 
"</td>";
    print 
"<td width='20'>&nbsp;</td>";
    print 
"<td valign='middle' align='center' width='100'>";
    print 
"New:&nbsp;<b>".$amazon["OFFERSUMMARY/LOWESTNEWPRICE/FORMATTEDPRICE"]."</b><br>";
    print 
"Used:&nbsp;<b>".$amazon["OFFERSUMMARY/LOWESTUSEDPRICE/FORMATTEDPRICE"]."</b><br>";
    print 
"<a href='".$amazon["DETAILPAGEURL"]."' class='nobr'>More Information</a>";
    print 
"</td>";
    print 
"</tr>";
    return (
$counter == 3);
  }
  
$version "2006-06-28";
  
$operation "ItemSearch";
  
$keyid "xxxx";
  
$tag "myid";
  
$responsegroup "Large";
  
$searchindex "SportingGoods";
  
$q str_replace(" ""+"$q);
  
$url "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&Version=".$version."&Operation=".$operation."&AWSAccessKeyId=".$keyid."&AssociateTag=".$tag."&ResponseGroup="$responsegroup."&SearchIndex=".$searchindex."&Keywords=".urlencode($q)."";
  
// start the table and div here
  
print "<div class='related'>";
  print 
"<table width='100%'>";
  
// parse the Amazon response to create rows MagicParser_parse($url,"myRecordHandler1","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");
  // end the table and div here
  
print "</table>";
  print 
"</div>";
?>

Test the above script and make sure that it works stand-alone by calling it with some keywords in the $q GET parameter; for example:

http://www.example.com/amazon.php?q=Golf+Clubs

Now, to integrate the script with the Price Tapestry product page; firstly, remove the line to include("MagicParser.php") as it is already included by Price Tapestry.

Then, I suggest that you save the script as html/amazon.php. To include the new script at the bootom of the product page, edit products.php to call the new script near to the bottom, so the last few lines would look like this:

<?php
  
// ... rest of script ...
  
if (isset($product)) require("html/product.php");
  if (isset(
$prices)) require("html/prices.php");
  require(
"html/amazon.php");
  require(
"html/footer.php");
?>

Hope this helps!
Regards,
David.

Submitted by dflsports on Thu, 2006-08-17 12:59

It was really late when I first tried this and I had no luck. But my brain was on overload and sleepy.

I'll give it a whirl today and report back. Thanks for the help :)

Don

Submitted by dflsports on Fri, 2006-08-18 01:19

Yep, it was a rather dumb error, had to chaneg the co.uk to a .com since I want amazon US, geesh, that was an easy fix.

Thanks for your help.

Don

Submitted by PHILDARV on Mon, 2006-08-21 09:14

Great code to integrate Amazon results - I've got this working in a slightly different format e.g. TomTom Go 510 but the prices are showing as £300.00 rather than £300.00

Does anyone have a solution for this?

Thanks

Submitted by crounauer on Mon, 2006-08-21 12:24

Hi PHILDARV,

All you would need to do is a search and replace...

<?php
ereg_replace
("[^0-9.]"''$data);
?>

and then hardcode the pond sign as "&pound" into you code.

Simon.

Submitted by support on Mon, 2006-08-21 12:40

Hi Phil,

Simon's suggestion should work fine - however i've just looked at your site and see that you are using the iso-8859-1 character set.

You are seeing funny characters in the response from Amazon because the Amazon data is probably utf-8 encoded. Another solution therefore is be to wrap the data in utf8_decode(), e.g.

<?php
  
print utf8_decode($data);
?>

Cheers,
David.

Submitted by PHILDARV on Mon, 2006-08-21 13:29

Thanks David

I've actually changed the whole site to utf8 and needed to change the £ sign in config to £ and it all now works a treat

Cheers

Phillip

Submitted by PHILDARV on Mon, 2006-08-21 13:57

Just another quick question about Amazon - is it possible to search 2 categories e.g. Kitchen & Electronics using one piece of code?

Submitted by support on Mon, 2006-08-21 15:24

I don't beleive so i'm afraid.

I just checked the docs and searchIndex only accepts a single value.

Cheers,
David.