Created
March 13, 2015 03:52
-
-
Save sanmai/98809a1f2d4912fbacdc to your computer and use it in GitHub Desktop.
Very simple and easy to use request signer for Amazon Product Advertising API. The only method returns ready-to-send URL
This file contains hidden or 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
<?php | |
/** | |
* Amazon Product Advertising API Signer | |
* | |
* @author Alexey Kopytko | |
* @license MIT | |
* | |
* Usage: | |
* | |
* $readyURL = AmazonSigner::signedURL([ | |
* 'Service'=>'AWSECommerceService', | |
* 'Operation'=>'ItemLookup', | |
* 'Version'=>'2011-08-01', | |
* 'ItemId'=>'059035342X', | |
* 'IdType'=>'ASIN', | |
* 'ResponseGroup'=>'Images,ItemAttributes,Offers', | |
* ]); | |
*/ | |
class AmazonSigner | |
{ | |
/* | |
* Product Advertising API ScratchPad: | |
* http://associates-amazon.s3.amazonaws.com/scratchpad/index.html | |
*/ | |
const ACCESS_KEY_ID = 'your-key'; | |
const SECRET_ACCESS_KEY = 'your-secret'; | |
const ASSOCIATE_TAG = 'your-tag'; | |
const REQUEST_PROTO = 'http://'; | |
const REQUEST_METHOD = 'GET'; | |
const SERVICE_HOST = 'webservices.amazon.com'; | |
const SERVICE_URI = '/onca/xml'; | |
/** | |
* @param array $request | |
* @return string URL | |
*/ | |
public static function signedURL($request) | |
{ | |
$request['AWSAccessKeyId'] = self::ACCESS_KEY_ID; | |
$request['AssociateTag'] = self::ASSOCIATE_TAG; | |
$request['Timestamp'] = gmdate('c'); | |
// Amazon wants comma-separated arrays | |
$request = array_map(function ($value) { | |
return is_array($value) ? implode(',', $value) : (string) $value; | |
}, $request); | |
// Amazon wants byte-sorted order for signatures | |
ksort($request); | |
$request['Signature'] = base64_encode(hash_hmac('sha256', implode("\n", [ | |
self::REQUEST_METHOD, | |
self::SERVICE_HOST, | |
self::SERVICE_URI, | |
http_build_query($request) | |
]), self::SECRET_ACCESS_KEY, true)); | |
return implode([ | |
self::REQUEST_PROTO, | |
self::SERVICE_HOST, | |
self::SERVICE_URI, | |
'?', http_build_query($request) | |
]); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment