php - cURL inventory update to Shopify API not working
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 (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] => 188.8.131.52 [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!
It looks like you have a spell mistake in your payload array,
$payload = array ( "variant" => array( "id" => $variantId, "inventory_quantity" => $inventoryQty ) );
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.