$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
使用测试xml字符串可以很直接地完成工作。
$requestXML = "<?xml version='1.0' encoding='utf-8'?><request>
<id>12345</id>
<email>eoin@dolepaddy.com</email><request>";
(https://magp.ie/2010/04/12/post-xml-with-https-authentication-using-php-curl/)
我将其与此结合:
function xml_encode($mixed, $domElement=null, $DOMDocument=null) {
if (is_null($DOMDocument)) {
$DOMDocument = new DOMDocument('1.0', 'utf-8');
$DOMDocument->formatOutput = true;
xml_encode($mixed, $DOMDocument, $DOMDocument);
echo $DOMDocument->saveXML();
}
else {
if (is_array($mixed)) {
foreach ($mixed as $index => $mixedElement) {
if (is_int($index)) {
if ($index === 0) {
$node = $domElement;
}
else {
$node = $DOMDocument->createElement($domElement->tagName);
$domElement->parentNode->appendChild($node);
}
}
else {
$plural = $DOMDocument->createElement($index);
$domElement->appendChild($plural);
$node = $plural;
if (rtrim($index,'s')!==$index && count($mixedElement)>1) {
$singular = $DOMDocument->createElement(rtrim($index, 's'));
$plural->appendChild($singular);
$node = $singular;
}
}
xml_encode($mixedElement, $node, $DOMDocument);
}
}
else {
$domElement->appendChild($DOMDocument->createTextNode($mixed));
}
}
}
从数组生成xml(将其保存到$ requestXML) (https://www.darklaunch.com/2009/05/23/php-xml-encode-using-domdocument-convert-array-to-xml-json-encode)
它与测试xml一起使用,但是不幸的是,不适用于生成的XML。我像这样连接$ requestXML:
$requestXML = "&data=";$requestXML .= xml_encode($data); // if the test xml comes here, everything works well
我猜生成的xml并不是真正的字符串,所以也许它不是与POSTFIELDS一起发送的?