phpxmlhttprequestamazon-advertising-api

Amazon Product Advertising API: XML request problems


I want get a "buy now" link and the actualy amazo price of a gaming product get with a game-title. For that i must use the "Search by title" ( http://docs.aws.amazon.com/AWSECommerceService/latest/DG/EX_SearchingbyTitle.html ).

I found a example for a XML-Request and try to build it for me.

The Example was:

    $AWSAccessKeyId = "";
$SecretAccessKey = "";

$ItemId = "0679722769"; // ASIN
$Timestamp = gmdate("Y-m-d\TH:i:s\Z");
$Timestamp = str_replace(":", "%3A", $Timestamp);
$ResponseGroup = "ItemAttributes,Offers,Images,Reviews";
$ResponseGroup = str_replace(",", "%2C", $ResponseGroup);

$String = "AWSAccessKeyId=$AWSAccessKeyId&
AssociateTag=xy&
ItemId=$ItemId&
Operation=ItemLookup&
ResponseGroup=$ResponseGroup&
Service=AWSECommerceService&
Timestamp=$Timestamp&
Version=2009-01-06";



$String = str_replace("\n", "", $String);

$Prepend = "GET\nwebservices.amazon.com\n/onca/xml\n";
$PrependString = $Prepend . $String;

$Signature = base64_encode(hash_hmac("sha256", $PrependString, $SecretAccessKey, True));  
$Signature = str_replace("+", "%2B", $Signature);
$Signature = str_replace("=", "%3D", $Signature);

$BaseUrl = "http://webservices.amazon.com/onca/xml?";
$SignedRequest = $BaseUrl . $String . "&Signature=" . $Signature;

$XML = simplexml_load_file($SignedRequest);

echo '<a href="'.$SignedRequest.'">XML</a><p>';
print_r ($XML);

That works perfect, but not with my search values. I edit the params and the request is broken.

My request:

    $AWSAccessKeyId = "";
$SecretAccessKey = "";

$ItemId = "Payday2"; // ASIN
$Timestamp = gmdate("Y-m-d\TH:i:s\Z");
$Timestamp = str_replace(":", "%3A", $Timestamp);
$ResponseGroup = "ItemAttributes";
$ResponseGroup = str_replace(",", "%2C", $ResponseGroup);

$String = "AWSAccessKeyId=$AWSAccessKeyId&
AssociateTag=xy&
Operation=ItemSearch&
SearchIndex=Game&
Title=$ItemId&
ResponseGroup=$ResponseGroup&
Service=AWSECommerceService&
Timestamp=$Timestamp&
Version=2009-01-06";



$String = str_replace("\n", "", $String);

$Prepend = "GET\nwebservices.amazon.com\n/onca/xml\n";
$PrependString = $Prepend . $String;

$Signature = base64_encode(hash_hmac("sha256", $PrependString, $SecretAccessKey, True));  
$Signature = str_replace("+", "%2B", $Signature);
$Signature = str_replace("=", "%3D", $Signature);

$BaseUrl = "http://webservices.amazon.com/onca/xml?";
$SignedRequest = $BaseUrl . $String . "&Signature=" . $Signature;

$XML = simplexml_load_file($SignedRequest);

echo '<a href="'.$SignedRequest.'">XML</a><p>';
print_r ($XML);

Why my request not work?

Thanks


Solution

  • There's two potential problems that I'm seeing here. The first is that "Game" doesn't seem to be a valid input for SearchIndex, but VideoGames is what you're looking for. See here for a list of all of them.

    (Outside the US can find applicable lists here)

    The other potential problem I see, and I'm not sure how flexible Amazon's API is with this, is that there is supposed to be a space in the game titled "Payday 2". Which actually brings up a more important consideration than fixing the space. Depending on how much control you have over the entry of the title into whatever you're coding, it might be more flexible to do a search based on keyword instead of title.