- Joined
- Oct 12, 2004
- Messages
- 697
- Reaction score
- 0
Amazon has recently announced (again) that it will update its Product Advertising API to require Associate Tag as part of the input parameters from October 25, 2011 onwards.
I have modified Mierendorffâs original PHP function to use cURL and to cater to the latest changes so that your PHP script will continue to work on and after October 25, 2011. (Note that the API version is 2011-08-01).
Anybody runing their script using Mierendorff's aws_signed_request() function can now update it to continue to work with the new API changes.
I have modified Mierendorffâs original PHP function to use cURL and to cater to the latest changes so that your PHP script will continue to work on and after October 25, 2011. (Note that the API version is 2011-08-01).
Code:
<?php
$public_key = "XXXXXXXX"; // Replace XXXXXXXX with your public key
$private_key = "XXXXXXXX"; // Replace XXXXXXXX with your private key
$associate_tag = 'associatetag-20';
$keywords = 'PHP';
$search_index = 'Books';
$xml = aws_signed_request(
'com',
array(
'Operation' => 'ItemSearch',
'Keywords' => $keywords,
'ResponseGroup' => 'Large,EditorialReview',
'SearchIndex' => $search_index
),
$public_key,
$private_key,
$associate_tag
);
print_r( $xml );
function aws_signed_request ( $locale, $params, $public_key, $private_key, $associate_tag ) {
/*
Copyright (c) 2009 Ulrich Mierendorff
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/*
Parameters:
$locale - the Amazon(r) locale (ca,com,co.uk,de,fr,jp)
$params - an array of parameters, eg. array("Operation"=>"ItemLookup",
"ItemId"=>"B000X9FLKM", "ResponseGroup"=>"Small")
$public_key - your "Access Key ID"
$private_key - your "Secret Access Key"
$associate_tag - your Associate Tag i.e. myamazontag-20
*/
// Some paramters
$method = 'GET';
$host = 'ecs.amazonaws.' . $locale;
$uri = '/onca/xml';
// Additional parameters
$params[ 'Service' ] = 'AWSECommerceService';
$params[ 'AWSAccessKeyId' ] = $public_key;
// GMT timestamp
$params[ 'Timestamp' ] = gmdate( "Y-m-d\TH:i:s\Z" );
// API version
$params[ 'Version' ] = '2011-08-01';
$params[ 'AssociateTag' ] = $associate_tag;
$params[ 'XMLEscaping' ] = 'Double';
// Sort the parameters
ksort( $params );
// Create the canonicalized query
$canonicalized_query = array();
foreach ( $params as $param => $value ) {
$param = str_replace( '%7E', '~', rawurlencode( $param ) );
$value = str_replace( '%7E', '~', rawurlencode( $value ) );
$canonicalized_query[] = $param . '=' . $value;
}
$canonicalized_query = implode( '&', $canonicalized_query );
// Create the string to sign
$string_to_sign = $method . "\n" . $host . "\n" . $uri . "\n" . $canonicalized_query;
// Calculate HMAC with SHA256 and base64-encoding
$signature = base64_encode( hash_hmac( 'sha256', $string_to_sign, $private_key, TRUE ) );
// Encode the signature for the request
$signature = str_replace( '%7E', '~', rawurlencode( $signature ) );
// Create request
$request = 'http://' . $host . $uri . '?' . $canonicalized_query . '&Signature=' . $signature;
// Do request
$ch = curl_init( $request );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
$response = curl_exec( $ch );
curl_close( $ch );
if ( FALSE === $response ) {
return FALSE;
} else {
// Parse XML
$xml = simplexml_load_string( $response );
if ( FALSE === $xml )
return FALSE; // no xml
else
return $xml;
}
}
?>
Anybody runing their script using Mierendorff's aws_signed_request() function can now update it to continue to work with the new API changes.