Shopify API 在 "Required parameter missing or invalid" 更新库存结果

Shopify API update inventory results in "Required parameter missing or invalid"

按照官方 Shopify API 示例,我创建了以下更新库存的请求:

[method] => PUT

[url] => https://<withheld>:<withheld>@test-shop-422.myshopify.com/admin/variants/1234567890.json

[headers] => Array (
     [0] => Accept: application/json
     [1] => Content-Type: application/json
     [2] => X-HTTP-Method-Override: PUT
)

[params] => {"variant":{"id":1234567890,"inventory_quantity":2,"old_inventory_quantity":1}}

[response] => {"errors":{"variant":"Required parameter missing or invalid"}}

我在论坛上找到了这个答案,但没有帮助:https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/what-does-errors-variant-required-parameter-missing-or-invalid-mean-270461

我做错了什么?

谢谢。

编辑:这是最终请求 CURL 选项和原始响应(<...> - 保留):

Array
(
    [CURLOPT_AUTOREFERER] => 1
    [CURLOPT_CAINFO] => <...>/cacert.pem
    [CURLOPT_CONNECTTIMEOUT] => 5
    [CURLOPT_FOLLOWLOCATION] => 1
    [CURLOPT_HEADER] => 1
    [CURLOPT_HTTPHEADER] => Array
        (
            [0] => Accept: application/json
            [1] => Content-Type: application/json
            [2] => X-HTTP-Method-Override: PUT
            [3] => Expect: 
            [4] => Referer: <...>/process_queue
            [5] => Content-Length: 47
        )

    [CURLOPT_MAXREDIRS] => 10
    [CURLOPT_POSTFIELDS] => {"variant":{"inventory_quantity_adjustment":1}}
    [CURLOPT_PUT] => 1
    [CURLOPT_RETURNTRANSFER] => 1
    [CURLOPT_SSL_VERIFYHOST] => 2
    [CURLOPT_SSL_VERIFYPEER] => 1
    [CURLOPT_TIMEOUT] => 5
    [CURLOPT_URL] => https://<...>:<...>@test-shop-422.myshopify.com/admin/variants/26020635016.json
    [CURLOPT_USERAGENT] => Mozilla/5.0
)

原始回复:

HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Oct 2016 01:23:05 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Frame-Options: DENY
X-ShopId: 9690230
X-ShardId: 7
X-Shopify-Shop-Api-Call-Limit: 1/40
HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT: 1/40
X-Stats-UserId: 0
X-Stats-ApiClientId: 1444946
X-Stats-ApiPermissionId: 31937062
X-XSS-Protection: 1; mode=block; report=/xss-report/<...>?source%5Baction%5D=update&source%5Bcontroller%5D=admin%2Fproduct_variants&source%5Bsection%5D=admin
X-Request-Id: <...>
X-Dc: chi2
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
X-Content-Type-Options: nosniff

{"errors":{"variant":"Required parameter missing or invalid"}}

这是一个 PHP 使用 cURL 的示例:

<?php
$ch = curl_init("https://key:pass@yourstore.myshopify.com/admin/variants/25930937097.json");
$variant = array('variant' => 
    array(
        'inventory_quantity' =>  2, 
        'old_inventory_quantity' => 1
    )
);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($variant)); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
print_r($response);

如果您仍然遇到问题,您介意分享您的一些代码吗?

如果使用 CURLOPT_PUT 或 CURLOPT_POST,问题是 curl 会覆盖 Content-Type header。需要删除这些并使用 CURLOPT_CUSTOMREQUEST.