NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page How To: Amazon AWS HMAC Signed Request Using PHP

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
6 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 08-03-2011, 03:09 AM THREAD STARTER               #1 (permalink)
NamePros Regular
 
Collector's Avatar
Join Date: Nov 2004
Posts: 240
Collector has a spectacular aura aboutCollector has a spectacular aura about
 



Lightbulb How To: Amazon AWS HMAC Signed Request Using PHP


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).
????: NamePros.com http://www.namepros.com/programming/725558-how-amazon-aws-hmac-signed-request.html

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.
Collector is offline   Reply With Quote
Reply

Tags
amazon, associate, hmac, php, signed


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 02:49 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger