发布XML顺序:无法检索查询字符串参数

时间:2013-02-05 23:10:25

标签: php xml post curl

我正在从数据库创建XML文件。随后,我正在获取该XML文件并尝试将其发布到外部网站。在处理了与解析错误有关的严重问题后,我发现问题是使用DOMDocument的结果,因此我将其转换为simpleXML。

我收到的错误是: Obj:SimpleXMLElement对象([@attributes] =>数组([Reason] =>无法检索查询字符串参数))参考=

//Save XML
$xmlDoc->save("file.xml");

//Convert DOMDocument to SimpleXML
$sxmlDoc = simplexml_load_file("file.xml");
print_r($sxmlDoc);

//Posting Order XML with PHP
$PartnerNumber = '1111'; 
$PartnerReference = $row['id'];
$OrderXml = '<Order Test="true">'.$sxmlDoc.'</Order>';
$urlConn = curl_init ("https://website.com/PostXmlOrder.axd?
   PartnerNumber=".$PartnerNumber."&PartnerReference=".urlencode($PartnerReference));
curl_setopt ($urlConn, CURLOPT_POST, 1);
curl_setopt ($urlConn, CURLOPT_HTTPHEADER, array("Content-type", "text/xml"));
curl_setopt ($urlConn, CURLOPT_POSTFIELDS, $sxmlDoc);
curl_setopt ($urlConn, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($urlConn, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($urlConn);
if (empty($result)) {
  print "ERROR: " . curl_error($urlConn) . "\n";
  exit;
}
// Parse the response
$xml = simplexml_load_string($result);
echo "Obj:\r\n";
print_r($xml);
echo "Reference=";
echo $xml->attributes()->Reference;

XML文件以SimpleXML格式输出到屏幕。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你正在做一些额外的循环。

让我们稍微回顾一下:您很可能需要将XML作为字符串POST,因此您不需要将DOMDocument转换为SimpleXML,而只需将其输出为字符串:

curl_setopt ($urlConn, CURLOPT_POSTFIELDS, $xmlDoc->saveXML());

另见DOMDocument::saveXML()

注意$xmlDoc转换为SimpleXMLElement,因此请从上方删除该代码。

// Create XML String
$xml = $xmlDoc->saveXML();

// Posting Order XML with Curl
$PartnerNumber    = '1111'; 
$PartnerReference = $row['id'];

$url = sprintf(
  "https://example.com/PostXmlOrder.axd?PartnerNumber=%s&PartnerReference=%s", 
   urlencode($PartnerNumber), 
   urlencode($PartnerReference)
);

$urlConn = curl_init($url);
curl_setopt_array(
    $urlConn, 
    array(
        CURLOPT_POST           => 1,
        CURLOPT_HTTPHEADER     => array("Content-type", "text/xml"),
        CURLOPT_POSTFIELDS     => $xml,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYPEER => 0,    
   )
);


$result = curl_exec($urlConn);
if (empty($result)) {
  print "ERROR: " . curl_error($urlConn) . "\n";
  exit;
}

// Parse the response
$sxml = simplexml_load_string($result);

echo "Obj:\r\n", print_r($sxml);
echo "Reference=", $sxml->attributes()->Reference;