Shopify XML Post in PHP
shopify
Solution
Thanks everyone. Based on this feedback, I was able to solve the problem. Major issues:
- The # is not needed as user-457786 suggested
- It needs to be a PUT method, not POST, which I found based on user-457786's link
A few other changes to the CURL:
$fp = tmpfile();
fwrite($fp, $xml);
fseek($fp, 0);
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_PUT, true);
curl_setopt($session, CURLOPT_BINARYTRANSFER, true);
curl_setopt($session, CURLOPT_INFILE, $fp); // file pointer
curl_setopt($session, CURLOPT_INFILESIZE, strlen($xml));
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
$response = curl_exec($session);
fclose($fp);
curl_close($session);
Problem
I am having trouble posting XML data using PHP and Curl to Shopify. I have: ``` $xml = '<?xml version="1.0" encoding="UTF-8"?><variant><id type="integer">260293006</id><fulfillment-service>manual</fulfillment-service><inventory-management>shopify</inventory-management><inventory-policy>deny</inventory-policy><sku>s136</sku><inventory-quantity type="integer">48</inventory-quantity><price>17.95</price></variant>'; $url = 'https://' . $API_KEY . ':' . $PASSWORD . '@' . $STORE_URL . '/admin/variants/#260293006.xml'; ``` My code is: ``` $session = curl_init(); curl_setopt($session, CURLOPT_URL, $url); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); curl_setopt($session, CURLOPT_FOLLOWLOCATION, true); curl_setopt($session, CURLOPT_MAXREDIRS, 3); curl_setopt($session, CURLOPT_POST, 1); curl_setopt($session, CURLOPT_POSTFIELDS, $xml); curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8')); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($session); echo $response; curl_close($session); ``` What is returned by Shopify page with this title: ``` <title>Shopify » Please Log In</title> ``` I think I am probably missing something obvious. Once I get this function to work, everything else should be easy to build. Thanks so much.