4

我在使用 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>

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

4

2 回答 2

5

谢谢大家。基于此反馈,我能够解决问题。大部分问题:

  1. 正如用户 457786 建议的那样,不需要 #
  2. 它需要是 PUT 方法,而不是 POST,我根据 user-457786 的链接找到了
  3. 对 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);
    
于 2012-12-12T07:00:14.763 回答
2

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

$url = 'https://' . $API_KEY . ':' . $PASSWORD . '@' . $STORE_URL . '/admin/variants/260293006.xml';
于 2012-12-11T17:46:32.687 回答