1

The code below results in an SignatureDoesNotMatch error and haven't had much luck on their forums or with support - any suggestions as to what is wrong with the below code?

<?php
error_reporting(E_ALL); 
ini_set("display_errors", 1); 
$AWS_ACCESS_KEY_ID = "";
$AWS_SECRET_ACCESS_KEY = "";

$base_url = "http://ecs.amazonaws.com/onca/xml?";
$url_params = array('Operation'=>"ItemSearch",'Service'=>"AWSECommerceService",
 'AWSAccessKeyId'=>$AWS_ACCESS_KEY_ID,'AssociateTag'=>"",
 'Version'=>"2011-08-01",'Availability'=>"Available",'Condition'=>"All",
 'ItemPage'=>"1",'ResponseGroup'=>"Images,ItemAttributes,EditorialReview",
 'Keywords'=>"Amazon");

// Add the Timestamp
$url_params['Timestamp'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());

// Sort the URL parameters
$url_parts = array();
foreach(array_keys($url_params) as $key)
    $url_parts[] = $key."=".$url_params[$key];
sort($url_parts);

// Construct the string to sign
$string_to_sign = "GET\necs.amazonaws.com\n/onca/xml\n".implode("&",$url_parts);
$string_to_sign = str_replace('+','%20',$string_to_sign);
$string_to_sign = str_replace(':','%3A',$string_to_sign);
$string_to_sign = str_replace(';',urlencode(';'),$string_to_sign);

// Sign the request
$signature = hash_hmac("sha256",$string_to_sign,$AWS_SECRET_ACCESS_KEY,TRUE);

// Base64 encode the signature and make it URL safe
$signature = base64_encode($signature);
$signature = str_replace('+','%2B',$signature);
$signature = str_replace('=','%3D',$signature);
$signature = str_replace(';',urlencode(';'),$signature);
$url_string = implode("&",$url_parts);
$url = $base_url.$url_string."&Signature=".$signature;
print $url;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

$xml_response = curl_exec($ch);
echo $xml_response; 
    ?>
4

1 回答 1

11

我收到了类似的错误消息:

  ["Code"]=> string(21) "SignatureDoesNotMatch"
  ["Message"]=> string(100) "Signature expired: 20130528T162908Z is now earlier than 20130528T162916Z (20130528T163416Z - 5 min.)"

检查我的服务器时钟后,我发现时间不对,在同步我的服务器时钟后,sudo ntpdate ntp.ubuntu.com我能够毫无问题地发送电子邮件,希望对您有所帮助。

于 2013-05-28T16:45:35.043 回答