3 function aws_signed_request($region, $params, $public_key, $private_key)
 
   6     Copyright (c) 2009 Ulrich Mierendorff
 
   8     Permission is hereby granted, free of charge, to any person obtaining a
 
   9     copy of this software and associated documentation files (the "Software"),
 
  10     to deal in the Software without restriction, including without limitation
 
  11     the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
  12     and/or sell copies of the Software, and to permit persons to whom the
 
  13     Software is furnished to do so, subject to the following conditions:
 
  15     The above copyright notice and this permission notice shall be included in
 
  16     all copies or substantial portions of the Software.
 
  18     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
  19     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
  20     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
  21     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
  22     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
  23     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
  24     DEALINGS IN THE SOFTWARE.
 
  29         $region - the Amazon(r) region (ca,com,co.uk,de,fr,jp)
 
  30         $params - an array of parameters, eg. array("Operation"=>"ItemLookup",
 
  31                         "ItemId"=>"B000X9FLKM", "ResponseGroup"=>"Small")
 
  32         $public_key - your "Access Key ID"
 
  33         $private_key - your "Secret Access Key"
 
  38     $host = "ecs.amazonaws.".$region;
 
  41     // additional parameters
 
  42     $params["Service"] = "AWSECommerceService";
 
  43     $params["AWSAccessKeyId"] = $public_key;
 
  45     $params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
 
  47     $params["Version"] = "2009-03-31";
 
  49     // sort the parameters
 
  52     // create the canonicalized query
 
  53     $canonicalized_query = array();
 
  54     foreach ($params as $param=>$value)
 
  56         $param = str_replace("%7E", "~", rawurlencode($param));
 
  57         $value = str_replace("%7E", "~", rawurlencode($value));
 
  58         $canonicalized_query[] = $param."=".$value;
 
  60     $canonicalized_query = implode("&", $canonicalized_query);
 
  62     // create the string to sign
 
  63     $string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
 
  65     // calculate HMAC with SHA256 and base64-encoding
 
  66     $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
 
  68     // encode the signature for the request
 
  69     $signature = str_replace("%7E", "~", rawurlencode($signature));
 
  72     $request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
 
  75     $response = @file_get_contents($request);
 
  77     if ($response === False)
 
  84         $pxml = simplexml_load_string($response);
 
  87             return False; // no xml