SimpleXMLElement和simplexml_load_string始终返回空白

时间:2019-02-25 14:18:54

标签: php xml soap

我这里有个情况。我有一个PHP脚本来向服务器发出SOAP请求。有2种标头格式可以发出请求。第一种格式可以成功运行,但是第二种格式却有问题。

这是我的第一种标头格式的php脚本:

<?php
    $soapUrl = "http://1.2.3.4:8002/";
    $soapUser = "USER";
    $soapPassword = "PASS";

    $xml_post_string1 = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rm="rm:soap">
                        <soapenv:Header> 
                            <header> 
                                <authentication> 
                                    <userPass> 
                                        <username>USER</username> 
                                        <password>PASS</password> 
                                    </userPass> 
                                </authentication> 
                            </header> 
                        </soapenv:Header>
                        <soapenv:Body>
                          ..................
                       </soapenv:Body>
                     </soapenv:Envelope>';

   $headers1 = array(
                    "Content-type: text/xml;charset=\"utf-8\"",
                    "Accept: text/xml",
                    "Cache-Control: no-cache",
                    "Pragma: no-cache",
                    "SOAPAction: http://1.2.3.4:8002/", 
                    "Content-length: ".strlen($xml_post_string1),

        $url = $soapUrl;

        $ch1 = curl_init();
        curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch1, CURLOPT_URL, $url);
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch1, CURLOPT_USERPWD, $soapUser.":".$soapPassword);
        curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch1, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch1, CURLOPT_POST, true);
        curl_setopt($ch1, CURLOPT_POSTFIELDS, $xml_post_string1);
        curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers1);

        $response1 = curl_exec($ch1); 
        curl_close($ch1);
        $response2 = str_replace("<SOAP-ENV:Body>","",$response1);
        $response3 = str_replace("</SOAP-ENV:Body>","",$response2);
        $parser1 = simplexml_load_string($response3);

        print("<pre>".print_r($parser1,true)."</pre>");
?>

上面的脚本对于第一种格式非常有效,但是当我尝试第二种XML格式时, parser1变量始终返回空白

enter image description here

第二种标题格式

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prov="http://www.telkomsel.com/soa/esb/Provisioning">
     <soapenv:Header>
       <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
         <wsu:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" wsu:Id="UsernameToken-17">
          <wsse:Username>ingw_prov</wsse:Username>
          <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Mongg0#18</wsse:Password>
          <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">O1Ke3ZnVI3leZMEqAxJ+3Q==</wsse:Nonce>
        </wsu:UsernameToken>
    </wsse:Security>
</soapenv:Header>

我已经尝试使用SimpleXMLElement(),但结果也和以前一样

当我尝试使用 POSTMAN 时,响应格式正确,如下所示:

enter image description here

这是我第一次将PHP用于SOAP函数。我真的需要你们的帮助,我在这里花了一天的时间来解决此问题。

谢谢。

* PS:对不起,我的英语不好

0 个答案:

没有答案