无法解析肥皂服务的卷曲响应(请愿后)

问题描述 投票:0回答:1

im正在尝试解析此请求的curl响应:

  $soapUrl = "https://soap_example_url.com/log_in";


    // xml post structure

    $xml_post_string = '
    <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:debisys-soap-services">
    <soapenv:Body>
        <urn:Login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <version xsi:type="xsd:string">'.$version.'</version>
            <userName xsi:type="xsd:string">'.$userName.'</userName>
            <password xsi:type="xsd:string">'.$password.'</password>
            <languageOption xsi:type="xsd:string">'.$language.'</languageOption>
        </urn:Login>
    </soapenv:Body>
    </soapenv:Envelope>
    ';


       $headers = array(
                    "Content-type: text/xml;charset=\"utf-8\"",
                    "Accept: text/xml",
                    "Cache-Control: no-cache",
                    "Pragma: no-cache",
                    "Content-length: ".strlen($xml_post_string),
                ); //SOAPAction: your op URL

        $url = $soapUrl;

        // PHP cURL  for https connection with auth
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        // converting
        $response = curl_exec($ch);

        curl_close($ch);

        // converting
        $response1 = str_replace("<soap:Body>","",$response);
        $response2 = str_replace("</soap:Body>","",$response1);

但是当我尝试解析“ $ response2”时,我什么也没得到:

$xml = simplexml_load_string($response2);
$json = json_decode(json_encode($xml));
echo json_encode($xml, JSON_PRETTY_PRINT);

这是我得到的:

SimpleXMLElement对象()

{}

print_r:stdClass对象()

[当我使用print_r ($response2);功能时,我会在浏览器中找到它:

<Login2Response><Version>01</Version><SiteID>xxxx</SiteID><ClerkId>xxxxx</ClerkId><ResponseCode>00</ResponseCode><ResponseMessage>user logged in</ResponseMessage></Login2Response>

并且当我使用“ json_encode”时,我得到了:

  "\n \n \n <LoginResponse><Version>01<\/Version><SiteID>xxxxx<\/SiteID><ClerkId>xxxxx<\/ClerkId><ResponseCode>00<\/ResponseCode><ResponseMessage>user logged in<\/ResponseMessage><\/LoginResponse><\/return>\n <\/ns1:Login2Response>\n <\/soapenv:Body>\n<\/soapenv:Envelope>\n"

所以我尝试将xml从我的浏览器复制/粘贴到这样的变量中:

$returned_xml = '<LoginResponse><Version>01</Version><SiteID>xxxxx</SiteID><ClerkId>xxxxx</ClerkId><ResponseCode>00</ResponseCode><ResponseMessage>user logged in</ResponseMessage></LoginResponse>';

而且它可以工作,但是我需要使用$ response2使其动态地实现,谢谢。

php json xml soap php-curl
1个回答
0
投票

已修复!

© www.soinside.com 2019 - 2024. All rights reserved.