亚马逊产品api调用响应:PHP中的SignatureDoesNotMatch

时间:2015-06-01 20:05:59

标签: php amazon-web-services

我正在拨打这样的电话在亚马逊进行一些搜索,我回复的回复说

 SignatureDoesNotMatchThe request signature we calculated does not match
 thesignature you provided. Check your AWS Secret Access Key and signing method. 
 Consult the service documentation for details

不确定为什么签名不匹配..     

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

  // 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\ecs.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);

      $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);
         curl_close($ch);

     echo $xml_response;
    ?>

我查看了与此相关的其他帖子,但不确定如何在同一页面中编写函数...我无法添加更多的php文件...所以我需要在这一页中包含所有代码。< / p>

1 个答案:

答案 0 :(得分:0)

尝试看看你是否得到相同的签名: http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html

如果您计算它的方式是正确的,那么您的机器和亚马逊服务器之间可能存在时钟差异。在这种情况下,您需要同步机器上的时间。