使用 PHP curl 批量调用 Amazon SP API

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

我正在尝试使用 PHP 和curl 来调用

getItemOffersBatch
的批处理过程来签名并拨打电话——我可以单独调用
getItemOffers
,没有任何问题。我运行了一批 2 并收到 403 Forbidden 错误 - 这是代码 (PHP 8):

json_encode($body, JSON_THROW_ON_ERROR);
$post = 'POST';
$body=json_encode($body, JSON_UNESCAPED_SLASHES);
//var_dump($body);
$urlbatch= "https://sellingpartnerapi-na.amazon.com/batches/products/pricing/v0/itemOffers". $body ;
//var_dump($urlbatch);
$ch = curl_init($urlbatch);
curl_setopt_array($ch, [CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true
,CURLOPT_CONNECTTIMEOUT => 5,]);
//  var_dump($header);
if ($post) curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
if ($header) curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('curl.log', 'a+'));
$out = curl_exec($ch);
if (curl_errno($ch)) exit('Error: ' . curl_error($ch));
if ($status !== null) $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return $out

有 ***** 的地方是有效的亚马逊私有数据。 在与亚马逊来回使用 POSTMAN 后,我让它在 POSTMAN 中工作,但我很难正确添加数据部分,这是来自 POSTMAN 的有效代码。

curl --location 'https://sellingpartnerapi-na.amazon.com/batches/products/pricing/v0/itemOffers' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'x-amz-access-token: ****' \
--header 'Authorization: AWS4-HMAC-SHA256 Credential=****/20240110/us-east-1/execute-api/aws4_request,SignedHeaders=host;user-agent;x-amz-access-token;x-amz-date,Signature=******' \
--data '{
"requests": [
    {
        "uri": "/products/pricing/v0/items/B000P6Q7MY/offers",
        "method": "GET",
        "MarketplaceId": "ATVPDKIKX0DER",
        "ItemCondition": "New",
        "CustomerType": "Consumer"
    },
    {
        "uri": "/products/pricing/v0/items/B001Q3KU9Q/offers",
        "method": "GET",
        "MarketplaceId": "ATVPDKIKX0DER",
        "ItemCondition": "New",
        "CustomerType": "Consumer"
    }
]

}'

我尝试添加数据到

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

但这没有用,任何帮助都会很棒,我几乎有了它

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

这就是答案——简单有时就是最好的

 $canonicalRequest = $method . "\n" // HTTP method
. $path . "\n" //  Path component of the URL
. $qs . "\n" // Query string component of the URL (without '?')
. 'host:' . HOST . "\n" // Header
. 'user-agent:' . USER_AGENT . "\n" // Header
. 'x-amz-access-token:' . $accessToken . "\n" // Header
. 'x-amz-date:' . $date . "\n" // Header
. "\n" // A newline is needed here after the headers
. 'host;user-agent;x-amz-access-token;x-amz-date' . "\n" // Header names
. hash('sha256', $data); // Hash of the payload (empty string okay)

function httpRequest($url,  $post = '', $header = null, &$status = null) {
$ch = curl_init($url);

curl_setopt_array($ch, [
    CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_CONNECTTIMEOUT => 5,
]);
if ($post) curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
if ($header) curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('curl.log', 'a+'));

$out = curl_exec($ch);

if (curl_errno($ch)) exit('Error: ' . curl_error($ch));
if ($status !== null) $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return $out;

}

没有将数据放入 canonicalRequest 中,因此它可以通过散列

© www.soinside.com 2019 - 2024. All rights reserved.