Created
April 6, 2014 13:47
-
-
Save standa/10006307 to your computer and use it in GitHub Desktop.
Get Amazon ASIN By SKU PHP example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get Amazon ASIN by SKU | |
* | |
* @param string $sku | |
* @return string Amazon ASIN | |
* @link http://docs.developer.amazonservices.com/en_UK/products/Products_GetMatchingProductForId.html | |
*/ | |
public function getAsinBySku($sku) | |
{ | |
// append the real SKU ending | |
$sku = $sku.'-XL-F-CUSTOM'; | |
/* | |
https://mws.amazonservices.com/Products/2011-10-01 | |
?AWSAccessKeyId=AKIAEXAMPLEFWR4TJ7ZQ | |
&Action=GetMatchingProductForId | |
&SellerId=A1IMEXAMPLEWRC | |
&SignatureVersion=2 | |
&Timestamp=2012-12-04T21%3A09%3A02Z | |
&Version=2011-10-01 | |
&Signature=ZhhdEXAMPLEiTy6k5etzw%2BIOCvbDrGop5u9EXAMPLE8%3D | |
&SignatureMethod=HmacSHA256 | |
&MarketplaceId=ATVPDKIKX0DER | |
&IdType=ISBN | |
&IdList.Id.1=9781933988665 | |
&IdList.Id.2=0439708184 | |
*/ | |
$amazonUrl = 'https://mws.amazonservices.co.uk/Products/2011-10-01'; | |
$marketplaceIds = $this->getContainer()->getParameter('amazon.sites.uk.marketplace_ids'); | |
$urlParams = array( | |
'AWSAccessKeyId' => $this->getContainer()->getParameter('amazon.sites.uk.aws_access_key_id'), | |
'Action' => 'GetMatchingProductForId', | |
'SellerId' => $this->getContainer()->getParameter('amazon.sites.uk.merchant_id'), | |
'SignatureVersion' => '2', | |
'Timestamp' => gmdate("Y-m-d\TH:i:s\Z"), | |
'Version' => '2011-10-01', | |
// 'Signature' => $signature, | |
'SignatureMethod' => 'HmacSHA256', | |
'MarketplaceId' => $marketplaceIds[0], | |
'IdType' => 'SellerSKU', | |
'IdList.Id.1' => $sku | |
); | |
ksort($urlParams); | |
// @todo http://docs.developer.amazonservices.com/en_UK/dev_guide/DG_ClientLibraries.html#DG_OwnClientLibrary__Signatures | |
// @todo AWS Client line 1283 | |
$stringToSign = "POST\n" | |
. "mws.amazonservices.co.uk\n" | |
. "/Products/2011-10-01\n" | |
. http_build_query($urlParams); | |
$secret = $this->getContainer()->getParameter('amazon.sites.uk.aws_secret_access_key'); | |
$hmacSignature = hash_hmac('sha256', $stringToSign, $secret); | |
echo "stringToSign: $stringToSign".PHP_EOL; | |
echo "HmacSHA256: ".$hmacSignature.PHP_EOL; | |
echo "Base64HMAC: ".base64_encode(hash_hmac('sha256', $stringToSign, $secret, true)).PHP_EOL; | |
$urlParams['Signature'] = base64_encode(hash_hmac('sha256', $stringToSign, $secret, true)); | |
$ch = curl_init(); | |
//set the url, number of POST vars, POST data | |
curl_setopt($ch, CURLOPT_URL, $amazonUrl); | |
curl_setopt($ch, CURLOPT_POST, count($urlParams)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($urlParams)); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
// curl_setopt($ch, CURLOPT_USERAGENT, 'Aardvark-Admin/0.1 (Language=PHP; Host=iconwallstickers.co.uk)'); | |
//execute post | |
$result = curl_exec($ch); | |
// print $result; | |
if (preg_match('/\<ASIN\>([A-Z0-9])\<\/ASIN\>/', $result, $match)) { | |
echo __METHOD__."(sku=$sku): Found ASIN: ".$match[1].PHP_EOL; | |
return $match[1]; | |
} else { | |
echo __METHOD__."(sku=$sku): ASIN Not found. Does the product exist?".PHP_EOL; | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment