Here's a standalone (namespaced) Amazon ItemLookup script to fetch product detail for a given ASIN - save in top level of Price Tapestry installation as amazon_ItemLookup.php
To use, with the variable $asin containing the ASIN number for the product; in the first instance use the following test code at the end of html/product.php to view all fields available for the response group(s) selected using:
Hi,
Here's a standalone (namespaced) Amazon ItemLookup script to fetch product detail for a given ASIN - save in top level of Price Tapestry installation as amazon_ItemLookup.php
<?php
require_once("includes/MagicParser.php");
$amazon_ItemLookup_AWSAccessKeyId = "YOUR_ACCESS_KEY_ID";
$amazon_ItemLookup_SecretAccessKey = "YOUR_SECRET_ACCESS_KEY";
$amazon_ItemLookup_AssociateTag = "YOUR_ASSOCIATE_TAG";
$amazon_ItemLookup_Host = "webservices.amazon.co.uk";
function amazon_ItemLookup_RecordHandler($item)
{
global $amazon_ItemLookup_Item;
$amazon_ItemLookup_Item = $item;
return TRUE;
}
function amazon_ItemLookup_Encode($text)
{
$encodedText = "";
$j = strlen($text);
for($i=0;$i<$j;$i++)
{
$c = substr($text,$i,1);
if (!preg_match("/[A-Za-z0-9\-_.~]/",$c))
{
$encodedText .= sprintf("%%%02X",ord($c));
}
else
{
$encodedText .= $c;
}
}
return $encodedText;
}
function amazon_ItemLookup_Sign($url,$secretAccessKey)
{
$url .= "&Timestamp=".gmdate("Y-m-d\TH:i:s\Z");
$urlParts = parse_url($url);
parse_str($urlParts["query"],$queryVars);
ksort($queryVars);
$encodedVars = array();
foreach($queryVars as $key => $value)
{
$encodedVars[amazon_ItemLookup_Encode($key)] = amazon_ItemLookup_Encode($value);
}
$encodedQueryVars = array();
foreach($encodedVars as $key => $value)
{
$encodedQueryVars[] = $key."=".$value;
}
$encodedQuery = implode("&",$encodedQueryVars);
$stringToSign = "GET";
$stringToSign .= "\n".strtolower($urlParts["host"]);
$stringToSign .= "\n".$urlParts["path"];
$stringToSign .= "\n".$encodedQuery;
if (function_exists("hash_hmac"))
{
$hmac = hash_hmac("sha256",$stringToSign,$secretAccessKey,TRUE);
}
elseif(function_exists("mhash"))
{
$hmac = mhash(MHASH_SHA256,$stringToSign,$secretAccessKey);
}
else
{
die("No hash function available!");
}
$hmacBase64 = base64_encode($hmac);
$url .= "&Signature=".amazon_ItemLookup_Encode($hmacBase64);
return $url;
}
function amazon_ItemLookup($asin)
{
global $amazon_ItemLookup_Item;
global $amazon_ItemLookup_AWSAccessKeyId;
global $amazon_ItemLookup_AssociateTag;
global $amazon_ItemLookup_SecretAccessKey;
global $amazon_ItemLookup_Host;
$url = "http://".$amazon_ItemLookup_Host."/onca/xml?Service=AWSECommerceService";
$url .= "&Version=2011-08-01";
$url .= "&Operation=ItemLookup";
$url .= "&AWSAccessKeyId=".$amazon_ItemLookup_AWSAccessKeyId;
$url .= "&AssociateTag=".$amazon_ItemLookup_AssociateTag;
$url .= "&ResponseGroup=Medium,OfferFull";
$url .= "&IdType=ASIN";
$url .= "&ItemId=".$asin;
$url = amazon_itemLookup_Sign($url,$amazon_ItemLookup_SecretAccessKey);
$xml = file_get_contents($url);
MagicParser_parse("string://".$xml,"amazon_ItemLookup_RecordHandler","xml|ITEMLOOKUPRESPONSE/ITEMS/ITEM/");
return (isset($amazon_ItemLookup_Item)?$amazon_ItemLookup_Item:FALSE);
}
?>
Edit the credentials and required Amazon API host for your locale at lines 2-6 as described on the Asynchronous Amazon and eBay API Modules page.
To use, with the variable $asin containing the ASIN number for the product; in the first instance use the following test code at the end of html/product.php to view all fields available for the response group(s) selected using:
<?php
require("amazon_ItemLookup.php");
$item = amazon_ItemLookup($asin);
print "<pre>".print_r($item,TRUE)."</pre>";
?>
Make a copy of the print_r() output and save to a local file for reference; and then to use individual fields as required; for example:
<?php
require("amazon_ItemLookup.php");
$item = amazon_ItemLookup($asin);
if (isset($item["ITEMATTRIBUTES/MANUFACTURER"]))
{
print "<p>Manufacturer: ".$item["ITEMATTRIBUTES/MANUFACTURER"]."</p>";
}
?>
Always use an isset() test for any field you wish to display as not all fields / attributes are populated for all products.
The above example requests the response groups Medium and OfferFull using the following code at line 80:
$url .= "&ResponseGroup=Medium,OfferFull";
The full response groups list can be found here...
Cheers,
David.
--
PriceTapestry.com