Php amazon api访问中的错误请求400错误

Php amazon api访问中的错误请求400错误,php,api,amazon,Php,Api,Amazon,编辑代码 foreach($parsed_xml->OperationRequest->Errors->Error as $error){ echo "Error code: " . $error->Code . "\r\n"; echo $error->Message . "\r\n"; echo "\r\n"; } } function printSearchResults($parsed_xml, $SearchIndex){ pr

编辑代码

foreach($parsed_xml->OperationRequest->Errors->Error as $error){
    echo "Error code: " . $error->Code . "\r\n";
    echo $error->Message . "\r\n";   echo "\r\n";
}
}
function printSearchResults($parsed_xml, $SearchIndex){
    print("<table>");
    if($numOfItems>0){
        foreach($parsed_xml->Items->Item as $current){
            print("<td><font size='-1'><b>".$current->ItemAttributes->Title."</b>");
            if (isset($current->ItemAttributes->Title)) {
                print("<br>Title: ".$current->ItemAttributes->Title);
            } elseif(isset($current->ItemAttributes->Author)) {
                print("<br>Author: ".$current->ItemAttributes->Author);
            } elseif(isset($current->Offers->Offer->Price->FormattedPrice)){
                print("<br>Price:".$current->Offers->Offer->Price->FormattedPrice);
            }else{
                print("<center>No matches found.</center>");
            }
        }
    }
}
if (!isset($params["Timestamp"])) {
    $params["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z');
}
ksort($params);
$pairs = array();
foreach ($params as $key => $value) {
    array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
}
$canonical_query_string = join("&", $pairs);
$string_to_sign = "GET\n".$endpoint."\n".$uri."\n".$canonical_query_string;
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $aws_secret_key, true));
$request_url =     'http://'.$endpoint.$uri.'?'.$canonical_query_string.'&Signature='.rawurlencode(     $signature);
echo "Signed URL: \"".$request_url."\"";
}
function fetchDataUsingAPI($UniqueBatchId) {
    echo $this->firstpageurl;
    if( isset($_POST['datafetchresume'])){
        $lastbatchdetails = GetTaskLastBatchDetails($_GET['taskid']);
        $UniqueBatchId =  $lastbatchdetails[0];
        $this->firstpageurl = $lastbatchdetails[1];
    }else {
        $this->firstpageurl = $this->ItemSearch($categorySplit[1],$UniqueBatchId);
    }
    SaveTaskLastBatchDetails($_GET['taskid'], $UniqueBatchId, $this- >firstpageurl);
    return $UniqueBatchId;
foreach($parsed_xml->OperationRequest->Errors->Error as$Error){
echo“错误代码:“.$Error->code.”\r\n;
echo$错误->消息。“\r\n”echo“\r\n”;
}
}
函数printSearchResults($parsed_xml,$SearchIndex){
打印(“”);
如果($numOfItems>0){
foreach($parsed_xml->Items->Item as$current){
打印(“$current->ItemAttributes->Title.”);
如果(isset($current->ItemAttributes->Title)){
打印(
标题:“.$current->ItemAttributes->Title); }elseif(isset($current->ItemAttributes->Author)){ 打印(
作者:“.$current->ItemAttributes->Author); }elseif(isset($current->Offers->Offer->Price->FormattedPrice)){ 打印(
价格:“.$current->Offers->Offer->Price->FormattedPrice”); }否则{ 打印(“未找到匹配项”); } } } } 如果(!isset($params[“Timestamp”])){ $params[“Timestamp”]=gmdate('Y-m-d\TH:i:s\Z'); } ksort($params); $pairs=array(); foreach($key=>$value的参数){ 数组_push($pairs,rawurlencode($key)。“=”.rawurlencode($value)); } $canonical_query_string=join(&,$pairs); $string\u to\u sign=“GET\n”。$endpoint.\n.$uri.\n.$canonical\u query\u string; $signature=base64_encode(hash_hmac(“sha256”,$string_to_sign,$aws_secret_key,true)); $request_url='http://'。$endpoint.$uri.'?'。$canonical_query_string.&Signature='。rawurlencode($Signature); 回显“签名URL:\”.$request\u URL.\”; } 函数fetchDataUsingAPI($UniqueBatchId){ echo$this->firstpageurl; 如果(isset($\u POST['datafetchresume'])){ $lastbatchdetails=GetTaskLastBatchDetails($\u GET['taskid']); $UniqueBatchId=$lastbatchdetails[0]; $this->firstpageurl=$lastbatchdetails[1]; }否则{ $this->firstpageurl=$this->ItemSearch($categorySplit[1],$UniqueBatchId); } SaveTaskLastBatchDetails($\u GET['taskid'],$UniqueBatchId,$this->firstpageurl); 返回$UniqueBatchId;
每个请求都需要计算签名并添加到URL。请查看我的PHP函数以添加签名

另外,使用CURL而不是
file\u get\u contents
。即使API返回了400错误,它也返回了XML。该XML可能包含有用的错误消息。如果使用CURL,您可以检测
400错误
,并仍然读取返回的页面内容。使用file\u get\u contents,400错误只会返回这是一个错误。(我可能记不起来了,但你会更喜欢卷发。)

有关
Amazon API错误代码的更多信息

在PHP代码中,我没有定义三个变量。MY_ASSOCIATE_ID、MY_PUBLIC_KEY、MY_PRIVATE_KEY必须由您使用适当的值定义。这两个键由amazon提供给您。您可以暂时在此处获取它们:

或者检查此页面顶部的“我的帐户”菜单,查找您的安全凭据。据报告,“新的和改进的”系统无法使用此API,因此我将在您仍然可以的情况下从上面的链接获取您的两个密钥。将它们存储在安全的地方


关联ID可以是任何内容—API不检查它。API使用URL参数生成带有公钥的签名,公钥包含在URL中,并且文件中的密钥与您提供的公钥关联。您在URL中提供的签名必须完全匹配,因此您的密钥和您的sig函数必须与他们的函数完全匹配。

谢谢你的回复,但我没有收到你的php代码的任何链接。我没有在这里提供任何php代码。我已经提供了一个解决方案,解释了为什么你的代码不起作用。谢谢你,我还添加了签名函数,但我对fetchDataUsingAPI的调用不起作用。你能在这里发布完整的代码吗?因为我是无法像这样理解。@Rashmi我今天离开办公室。明天会检查它。或者电子邮件meThis不是问题。,。请格式化代码,缩小问题范围,并添加真实的句子。