PHP - 尝试更新产品时JSON请求无效

时间:2014-12-10 19:20:02

标签: php json shopify

我正在使用PHP访问shopify API,但是当我尝试更新产品记录及其变体数量时,我只能找到产品数据,但它并不起作用。奇怪的是,我得到了一个json响应,详细说明了我正在尝试更新的产品的信息,它总是指定inventory_quantity值和old_inventory_quantity值是相同的,所以如果我发送一个新数量或1,旧数量为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);
}

1 个答案:

答案 0 :(得分:0)

如果您尝试单独更新库存,则有效内容数组应如下所示

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

您的网址应如下所示

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

使用PUT方法以及$inventory数组作为参数调用上述网址。

我有一个工作演示,它使用上述方法管理广告资源。

您可以使用demo:password

http://sarkutils.in/swpanel看到该演示