用PHP购买XML Post

时间:2012-12-11 08:19:48

标签: shopify

我无法使用PHP和Curl发布XML数据到Shopify。我有:

$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';

我的代码是:

$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);

Shopify页面返回的内容为:

<title>Shopify &raquo; Please Log In</title>

我想我可能错过了一些明显的东西。一旦我使这个功能工作,其他一切都应该很容易构建。非常感谢。

2 个答案:

答案 0 :(得分:5)

谢谢大家。根据这些反馈,我能够解决问题。主要问题:

  1. 用户-457786建议
  2. 不需要#
  3. 它需要是一个PUT方法,而不是POST,我根据用户-457786的链接找到了
  4. 对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);
    

答案 1 :(得分:2)

我认为#中不需要$url,它应该是

$url = 'https://' . $API_KEY . ':' . $PASSWORD . '@' . $STORE_URL . '/admin/variants/260293006.xml';