使用PHP和cURL使用HTTPS身份验证发布XML
$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);

This is working pretty straight forward with a test xml string.

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

I am combining this with this:

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

to generate the xml from an array (saving it to $requestXML) (https://www.darklaunch.com/2009/05/23/php-xml-encode-using-domdocument-convert-array-to-xml-json-encode)

It works with the test xml, but unfortunately not with generated XML. I concat the $requestXML like this:

$requestXML = "&data=";$requestXML .= xml_encode($data); // if the test xml comes here, everything works well

I guess the generated xml is not really a string, so that's maybe it is not send with the POSTFIELDS?

dongsuikai8286
dongsuikai8286 写关于帮助...我没有在xml_encode()中给出任何回馈。这现在有效://echo$DOMDocument->saveXML();return$DOMDocument->saveXML();
接近 2 年之前 回复
douyiken0968
douyiken0968 谢谢你的帮助错误。我已经用PHP编写了很长时间。的var_dump(xml_encode($数据));->NULLechoxml_encode($data);->向我展示了一个很好的格式化XML我之前复制了这个格式化的XML并将其用作$testXML=“<generatedXMLstuff>...”;哪个有效。我之前用$testXML发送了原始代码和额外的东西。领先且由于接收服务器需要的额外参数。
接近 2 年之前 回复
dtgta48604
dtgta48604 您的原始代码直接将$requestXML作为CURLOPT_POSTFIELDS发送,现在您正尝试发送一些表格&data=...-为什么?为什么一个领先的&,之前发生了什么?
接近 2 年之前 回复
dongran1779
dongran1779 “我想生成的xml实际上不是一个字符串-不要猜-验证!var_dump显示您正在处理的数据类型。
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐