如何将SOAP响应转换为XML或JSON格式?

Chi*_*235 5 php xml json soap

<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:body>
        <ns1:mbillcommandresponse xmlns:ns1="http://www.mysoap.com/SoapService/">
            <ReturnValues>
                <name>status</name>
                <value>TEHNICAL_ERROR</value>
            </ReturnValues>
            <ReturnValues>
                <name>description</name>
                <value>Please contact your administrator</value>
            </ReturnValues>
        </ns1:mbillcommandresponse>
    </soapenv:body>
</soapenv:envelope>
Run Code Online (Sandbox Code Playgroud)

上面我得到了我的CURL回复.这是我的PHP代码:

    $response = curl_exec($ch);
    $xml = simplexml_load_string($response);
    //$result = $xml->xpath('//name'); //echo "<pre>"; print_r($result); exit;
    $xml->registerXPathNamespace('ns1', 'http://www.mysoap.com/SoapService/');
    foreach ($xml->xpath('//returnvalues') as $item) {
        $json = json_encode($item);
        $convrt_arr = json_decode($json, true);
        break;
    }
    print_r($json); exit;
Run Code Online (Sandbox Code Playgroud)

在我上面的代码中,我得到了空的json.请你帮助我好吗.

jst*_*tur 2

你的问题编辑是一个很大的线索。您将 XML 元素从小写更改为 CamelCase。改变:

foreach ($xml->xpath('//returnvalues') as $item) {
Run Code Online (Sandbox Code Playgroud)

到:

foreach ($xml->xpath('//ReturnValues') as $item) {
Run Code Online (Sandbox Code Playgroud)

它会起作用的。Xpath 查询区分大小写。