对Shopify API的cURL库存更新无效

时间:2015-06-26 20:59:40

标签: php json api curl shopify

请查看我的cURL测试代码,如果您发现为什么执行该代码不会更新我的产品Variant的库存,请告诉我。只有一个产品具有Variant数据,没有单独的产品变体。所以它应该是非常简单的,对吧?

已安装cURL并且在我的包装盒上工作正常,并且还验证了API密钥/令牌。

这是我的代码

//Set up access

$apiKey = "[my api key]";
$pwd = "[my pwd]";

$baseUrl = $apiKey . $pwd ."@sensible-herbs.myshopify.com/admin/";

//Set up test data  

  $variantId = '3744859331';
  $inventoryQty = '50';

//Set up JSON payload

$payload = array (            
   "variant" => array("id" => $variantId,
   " nventory_quantity" => $inventoryQty
   )               
);  

$payload = json_encode($payload, JSON_NUMERIC_CHECK);
echo 'JSON payload: ' .$payload  ."<p>";

$putUrl = $baseUrl ."variants/" ."$variantId".".json";
echo 'url: ' .$putUrl ."<hr>";

$session = curl_init(); 

curl_setopt($session, CURLOPT_URL, $putUrl);
curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 30); //seconds to allow for connection
curl_setopt($session, CURLOPT_TIMEOUT, 30); //seconds to allow for cURL commands
curl_setopt($session, CURLOPT_HEADER, true); //include header info in return value ? 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); //return response as a string
curl_setopt($session, CURLOPT_PUT, 1); 
curl_setopt($session, CURLOPT_POSTFIELDS, $payload); 
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));

$data = curl_exec($session);

print_r(curl_getinfo($session));

curl_close($session);

echo $data; // session data  

我得到的回应是:

数组([url] =&gt; https://[apikey+password]@sensible-herbs.myshopify.com/admin/variants/3744859331.json [content_type] =&gt; [http_code] =&gt; 0 [header_size] =&gt; 0 [request_size] =&gt; 0 [filetime] =&gt; - 1 [ssl_verify_result] =&gt; 0 [redirect_count] =&gt; 0 [total_time] =&gt; 0.156 [namelookup_time] =&gt; 0.078 [connect_time] =&gt; 0.156 [pretransfer_time] =&gt; 0 [size_upload] =&gt; 0 [size_download] =&gt; 0 [speed_download] =&gt; 0 [speed_upload] =&gt; 0 [download_content_length] =&gt; -1 [upload_content_length] =&gt; -1 [starttransfer_time] =&gt; 0 [redirect_time] =&gt; 0 [certinfo] =&gt;数组()[primary_ip] =&gt; 23.227.38.71 [primary_port] =&gt; 443 [local_ip] =&gt; 192.168.0.10 [local_port] =&gt; 50573 [redirect_url] =&gt;)< / p>

所以看起来它正在连接但是有效载荷没有得到PUT ......或者我只是不明白API / URL的要求是什么......或者可能有一些隐藏或丢失的开关或权限我需要设置除APIKey之外的其他代码...我希望我知道!

提前致谢和namaste!

凯拉斯

1 个答案:

答案 0 :(得分:0)

您的有效负载数组中似乎有一个拼写错误,inventory_quantity而不是nventory_quantity

$payload = array (            
   "variant" => array(
       "id" => $variantId,
       "inventory_quantity" => $inventoryQty
   )               
);