1

I am attempting to use the Amazon MWS GetMatchingProductForId API, and I am stuck on trying to get information for multiple isbn's. The format needs to be in the form of: IdList.Id.1=isbn[x], where isbn[x] would be my array. I need to be able to list them in a list of 20 (ie. IdList.Id.1=, IdList.Id.2= ...IdList.Id.20= ). I thought I could use implode($isbn) like this:

$count=0;
function ProductId_xml($searchTerm) {

$params = array(
    'AWSAccessKeyId' => AWS_ACCESS_KEY_ID,
    'Action' => "GetMatchingProductForId",
    'SellerId' => MERCHANT_ID,
    'SignatureMethod' => "HmacSHA256",
    'SignatureVersion' => "2",
    'Timestamp'=> gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()),
    'Version'=> "2011-10-01",
    'MarketplaceId' => MARKETPLACE_ID,
    'IdType' => "ISBN",
    );

$id=array(explode(',',$searchTerm));
foreach ($id as $newId)
{
$count .= $count +1;    
    $params += array('IdList.Id.'.$count => $newId);

} //end of foreach      
// Sort the URL parameters
$url_parts = array();
foreach(array_keys($params) as $key)
    $url_parts[] = $key . "=" . str_replace('%7E', '~', rawurlencode($params[$key]));
sort($url_parts);

// Construct the string to sign
$url_string = implode("&", $url_parts);
$string_to_sign = "GET\nmws.amazonservices.com\n/Products/2011-10-01\n" . $url_string;

// Sign the request
$signature = hash_hmac("sha256", $string_to_sign, AWS_SECRET_ACCESS_KEY, TRUE);

// Base64 encode the signature and make it URL safe
$signature = urlencode(base64_encode($signature));

$url = "https://mws.amazonservices.com/Products/2011-10-01" . '?' . $url_string . "&Signature=" . $signature;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($ch);

    $parsed_xml = simplexml_load_string($response);

return ($parsed_xml);
}
}
?>

$searchterm is my array of isbn numbers, but that only lists them as a string of isbn's. I know that this works if I only use one isbn at a time, but I need to run them in batches of 20 to speed up my process. I am not sure where to go from here. Any help would be greatly appreciated. EDIT: I have updated the function so that it will include the batch of 20 isbn's in $params, but I am not sure that the way I added it is correct.

Jim
  • 1,315
  • 4
  • 17
  • 45
  • I'm not sure either, but possible this existing solution will give you some pointers how to go on from where you are: [Fastest way to add prefix to array keys?](http://stackoverflow.com/questions/2607595/fastest-way-to-add-prefix-to-array-keys) – hakre Oct 08 '12 at 13:42

1 Answers1

3
function ProductId_xml($searchTerm) {
$count=0;
$id=array(explode(',',$searchTerm));
foreach ($id as $newId)
{
$count .= $count +1;
$params = array(
'AWSAccessKeyId' => AWS_ACCESS_KEY_ID,
'Action' => "GetMatchingProductForId",
'SellerId' => MERCHANT_ID,
'SignatureMethod' => "HmacSHA256",
'SignatureVersion' => "2",
'Timestamp'=> gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()),
'Version'=> "2011-10-01",
'MarketplaceId' => MARKETPLACE_ID,
'IdType' => "ISBN",
'IdList.Id.'.$count => $newId,
);
run query here;
}

Haven't tested but that should work, it will explodes the $searchTerm to an array by commas, then iterates through them. I don't know the Amazon API this will run them one at a time through the query, but at least it allows you to automate the process.

Rick Calder
  • 18,310
  • 3
  • 24
  • 41
  • Rick, thanks for the assistance, however I am getting an error back that states parameter IdList.Id.01 failed a validation check: value cannot be empty. I think it is because it is 01 and not just 1. Any thoughts? I have posted the rest of my function for you to look at. – Jim Oct 08 '12 at 13:54
  • Strange, that error says that the id is empty, try printing out params array and see what's in it. – Rick Calder Oct 08 '12 at 18:52
  • Rick, I was able to get it to work with a little alteration. Thanks for the help though. – Jim Oct 08 '12 at 19:50