doubingjiu3199 2015-06-26 20:59
浏览 73

对Shopify API的cURL库存更新无效

Please review my cURL test code and let me know if you see any reason why executing it does not update the inventory of my product Variant. There is only one Product that has it's Variant data, there are not separate Variants of the Product. So it should be pretty straight-forward, right?

cURL is installed and is working fine on my box, and the API key / token are also verified.

Here is my code


//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  

The response I am getting is:

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

So it looks like it's connecting but either the payload is not getting PUT...or I just don't understand what the API / URL requirements are for this... or maybe there is some hidden or missing switch or permission I need to set up other than the APIKey generation... I wish I knew!

Thanks in advance and namaste!

Kailas

  • 写回答

1条回答 默认 最新

  • doulao2128 2015-06-27 04:43
    关注

    It looks like you have a spell mistake in your payload array, inventory_quantity not nventory_quantity

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算