亚马逊广告卷曲 PHP

时间:2021-04-17 07:25:15

标签: php php-curl amazon-product-api

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

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

我得到的错误是“Curl 错误: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);

请问有什么问题吗?

0 个答案:

没有答案