PHP - 尝试更新产品时 JSON 请求不起作用


PHP - JSON request not working when attempting to update product

我正在使用PHP访问shopify API,我可以获取产品数据,但是当我尝试更新产品记录及其变体数量编号时,它不起作用。奇怪的是,我收到一个json响应,详细说明了有关我尝试更新的产品的信息,并且它始终指定inventory_quantity值和old_inventory_quantity值相同,因此,如果我发送新数量或1并且旧数量为0,则响应表明它们都是0,告诉我没有更新任何内容。我编码的 json 在转储时如下所示:

"{
"product":
{
"id":127417494,"variants":
{
"id":288267528,
"inventory_quantity":2
}
}
}"

我得到的回应是:

" ["created_at"]=> string(25) "2013-03-18T16:47:45-07:00" ["handle"]=> string(23) "peacock-herl-small-pack" ["id"]=> int(127417488) ["product_type"]=> string(15) "Tying Materials" ["published_at"]=> string(25) "2013-03-18T16:47:00-07:00" ["published_scope"]=> string(6) "global" ["template_suffix"]=> string(0) "" ["title"]=> string(25) "Peacock Herl - Small Pack" ["updated_at"]=> string(25) "2014-12-09T18:15:07-08:00" ["vendor"]=> string(19) "Pacific Fly Fishers" ["tags"]=> string(8) "Feathers" ["variants"]=> array(1) { [0]=> array(22) { ["barcode"]=> NULL ["compare_at_price"]=> NULL ["created_at"]=> string(25) "2013-03-18T16:47:45-07:00" ["fulfillment_service"]=> string(6) "manual" ["grams"]=> int(45) ["id"]=> int(288267518) ["inventory_management"]=> string(7) "shopify" ["inventory_policy"]=> string(4) "deny" ["option1"]=> string(5) "Small" ["option2"]=> NULL ["option3"]=> NULL ["position"]=> int(1) ["price"]=> string(4) "3.50" ["product_id"]=> int(127417488) ["requires_shipping"]=> bool(true) ["sku"]=> string(12) "762820007750" ["taxable"]=> bool(true) ["title"]=> string(5) "Small" ["updated_at"]=> string(25) "2014-12-08T15:23:28-08:00" ["inventory_quantity"]=> int(5) ["old_inventory_quantity"]=> int(5) ["image_id"]=> NULL } } ["options"]=> array(1) { [0]=> array(4) { ["id"]=> int(152687222) ["name"]=> string(5) "Title" ["position"]=> int(1) ["product_id"]=> int(127417488) } } ["images"]=> array(1) { [0]=> array(7) { ["created_at"]=> string(25) "2014-12-08T15:23:23-08:00" ["id"]=> int(1077719361) ["position"]=> int(1) ["product_id"]=> int(127417488) ["updated_at"]=> string(25) "2014-12-08T15:23:25-08:00" ["src"]=> string(93) "https://cdn.shopify.com/s/files/1/0211/7110/products/peacock-herl-small-pack.jpg?v=1418081005"; ["variant_ids"]=> array(0) { } } } ["image"]=> array(7) { ["created_at"]=> string(25) "2014-12-08T15:23:23-08:00" ["id"]=> int(1077719361) ["position"]=> int(1) ["product_id"]=> int(127417488) ["updated_at"]=> string(25) "2014-12-08T15:23:25-08:00" ["src"]=> string(93) "https://cdn.shopify.com/s/files/1/0211/7110/products/peacock-herl-small-pack.jpg?v=1418081005"; ["variant_ids"]=> array(0) { } } } } array(1) { ["product"]=> array(16) { ["body_html"]=> string(169) "
Beautiful peacock sword that is often used for tails on patterns like Zug Bugs and Spruce Fly streamers. Also used in many atlantic salmon and steelhead patterns.
我已经一遍

又一遍地检查了 id,它似乎击中了正确的端点,但无论出于何种原因,它都不会接受新的数量值,我不知道我是否缺少必需的参数还是什么,但我几乎处于死胡同。

如果有人好奇我的 cUrl 请求看起来像这样

if ($data != null) {
    curl_setopt_array($ch, array(        
    CURLOPT_CUSTOMREQUEST  => 'PUT', //sets method if specified
    CURLOPT_RETURNTRANSFER => true, //Causes curl_exec() to return the response
    CURLOPT_HEADER         => false, //Suppress headers from returning in curl_exec()
    CURLOPT_HEADERFUNCTION => array($this, 'header_callback'), //call header_callback upon receiving headers
    CURLOPT_HTTPHEADER => array('Content-Type: application/json','Content-Length: ' . strlen($data)),
    CURLOPT_POSTFIELDS => $data,        
    ));
  $this->response = curl_exec($ch);
  // close curl
  curl_close($ch);
  // return json data
  return json_decode($this->response, true);
}

如果您尝试单独更新清单,则有效负载数组应如下所示

$inventory = array (            
    "variant"=>array (
        "id"=>{{'your_variant_id'}},
        "inventory_quantity"=>{{'your_inventory_quantity'}}
    )               
);

您的网址应如下所示

$url = "variants/".{{'your_variant_id'}}.".json";

使用PUT方法调用上面的 url 以及$inventory数组作为参数。

我有一个工作演示,它使用上述方法来管理库存。

您可以在 http://sarkutils.in/swpanel 看到该演示,demo:password