亚马逊广告 Curl PHP

问题描述 投票:0回答:1

亚马逊为广告 API 提供了一个暂存器,其中包含 PHP 和curl 代码。不幸的是,PHP 代码使用 fopen - 这在我的托管服务器上不好,远程 url 打开已关闭。

到目前为止,我已经注释掉了 fopen 部分(这在我的家庭服务器上工作,所以我知道 $headers 数组没问题!$payload 是 json

我收到的错误是“Curl error: 500 由于某些未知错误、异常或失败,请求处理失败。请重试。”

 $headers = $awsv4->getHeaders ();
$payload="{"
        ." \"Keywords\": \"".stripslashes($_POST['book'])."\","
        ." \"Resources\": ["
        ."  \"CustomerReviews.StarRating\","
        ."  \"Images.Primary.Large\","
        ."  \"Images.Primary.Small\","
        ."  \"ItemInfo.ByLineInfo\","
        ."  \"ItemInfo.ContentInfo\","
        ."  \"ItemInfo.Classifications\","
        ."  \"ItemInfo.ExternalIds\","
        ."  \"ItemInfo.Features\","
        ."  \"ItemInfo.ProductInfo\","
        ."  \"ItemInfo.TechnicalInfo\","
        ."  \"ItemInfo.Title\","
        ."  \"Offers.Listings.Price\""
        ." ],"
        ." \"PartnerTag\": \"keepchickens-21\","
        ." \"PartnerType\": \"Associates\","
        ." \"Marketplace\": \"www.amazon.co.uk\""
        ."}";
    $headerString = "";
   
   
    foreach ( $headers as $key => $value ) {
        $headerString .= $key . ': ' . $value . "\r\n";
        
    }
    $params = array (
            'http' => array (
                'header' => $headerString,
                'method' => 'POST',
                'content' => $payload
            )
        );
    $stream = stream_context_create ( $params );
    /*

    
    $fp = @fopen ( 'https://'.$host.$uriPath, 'rb', false, $stream );

    if (! $fp) {
        throw new Exception ( "Exception Occured" );
    }
    $response = @stream_get_contents ( $fp );
    if ($response === false) {
        throw new Exception('<p>Nothing found</p>');
    }
    */
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, 'https://'.$host.$uriPath);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    $response=curl_exec($ch);
     echo 'Curl error: ' . curl_error($ch);
     echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    print_r($response);
    $data=json_decode($response);

请问有什么问题吗?

php php-curl amazon-product-api
1个回答
0
投票

我发现,如果您从亚马逊的 PHP 示例中输出 $headers 的内容,则返回的唯一参数是授权(而不是主机、内容类型等)。此外,$header 输出使用逗号,但 Amazon 的 curl 示例没有 => https://webservices.amazon.com/paapi5/documentation/quick-start/using-curl.html。一旦解决了这个问题,我就不再遇到 500 错误(但现在出现 400 错误 =>)。

HTTP/1.1 400 错误请求服务器:服务器日期:2024 年 3 月 9 日星期六 20:22:52 GMT 内容类型:text/html 传输编码:分块连接:保持活动 x-amz-rid:6V3GN7J90XWPVRDQ47WC 变化:内容类型、接受编码、用户代理上次修改时间:2024 年 3 月 7 日星期四 10:23:39 GMT ETag:“209-6130f780cc984” 接受范围:字节 严格传输安全:max-age=47474747;包括子域;预载

(对我来说)最大的障碍似乎是将 fopen 的 php 示例中的标题和字段格式化为curl(我认为这导致了 400 错误)。这是我到目前为止所取得的成果,所以希望这至少可以帮助您克服 500 错误。

$authorization = $awsv4->getHeaders ();
$authorization = str_replace (","," ", $authorization);
print_r ($authorization);

$amzdate =  gmdate ( "Ymd\THis\Z" );

$data_string = array(
    "Keywords" => "xxxx",
    "PartnerTag" => "xxxx",
    "PartnerType" => "Associates",
    "Marketplace" => "www.amazon.com"
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$host.$uriPath);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Host: webservices.amazon.com',
    'Accept: application/json, text/javascript',
    'Accept-Language: en-US',
    'Content-Type: application/json; charset=UTF-8',
    'X-Amz-Date: ' . $amzdate,
    'X-Amz-Target: com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems',
    'Content-Encoding: amz-1.0',
    'Authorization: ' . $authorization,
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_string));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

$response=curl_exec($ch);

curl_close($ch);
© www.soinside.com 2019 - 2024. All rights reserved.