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

时间:2023-01-15 08:54:38
<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>

Above I got response in my CURL response. Here is my PHP code:

上面我在我的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;

On my above code I got empty json. Could you please help me.

在我的上面的代码我得到空json。你可以帮我吗。

2 个解决方案

#1


2  

Your question edit is a big clue. You changed the XML from having lowercase to CamelCase elements. Change:

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

foreach ($xml->xpath('//returnvalues') as $item) {

to:

至:

foreach ($xml->xpath('//ReturnValues') as $item) {

and it will work. Xpath queries are case sensitive.

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

#2


0  

Hope this helps

希望这可以帮助

$response = '<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>';
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);

$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//returnvalues');
$json = json_encode((array)$body); 
print_r($json);

#1


2  

Your question edit is a big clue. You changed the XML from having lowercase to CamelCase elements. Change:

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

foreach ($xml->xpath('//returnvalues') as $item) {

to:

至:

foreach ($xml->xpath('//ReturnValues') as $item) {

and it will work. Xpath queries are case sensitive.

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

#2


0  

Hope this helps

希望这可以帮助

$response = '<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>';
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);

$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//returnvalues');
$json = json_encode((array)$body); 
print_r($json);