Warm tip: This article is reproduced from serverfault.com, please click

parse the soap xml response to array

发布于 2017-11-30 11:06:40

I had used curl to call the soap server and i got the response like this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GenerateAuthPasswordResponse xmlns="NepalTelecom.AuthGateway">
            <GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
            <ResultCode>1</ResultCode>
        </GenerateAuthPasswordResponse>
    </soap:Body>
</soap:Envelope>

and when i tried to parse the soap xml as:

$response = $this->SoapModel->soapCall($xml , $this->vas_wsdl_url);
 $obj = simplexml_load_string($response);
 echo $obj;die();

[Note: where $response is the above soap response provided in soap xml]

and i get the $obj as some error like this:

Severity: Warning
Message:  simplexml_load_string(): namespace warning : xmlns: URI NepalTelecom.AuthGateway is not absolute

please any body could help so fix this issue. Thank you in advance.

Questioner
Javed Ali
Viewed
0
Alex 2017-11-30 20:13:04

Try this.

<?php

$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GenerateAuthPasswordResponse >
            <GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
            <ResultCode>1</ResultCode>
        </GenerateAuthPasswordResponse>
        <GenerateAuthPasswordResponse >
            <GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
            <ResultCode>1</ResultCode>
        </GenerateAuthPasswordResponse>
    </soap:Body>
</soap:Envelope>';

$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap']);
$res = $soap->Body->children();

print_r($res);