Parse soap response using PHP

2019-10-03 04:22发布

I am trying to get all the elements and values as array from the below soap response. But I am only getting elements but no values.

I have used this parser to get the elements and values. Thanks in advance.

Here is the code I am using,

  $response = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <toy:SearchReq xmlns:toy="www.something.com/toy" xmlns:gen="www.something.com/gen">
         <gen: manufacturedIn="SomePlace"/>
         <toy:SearchToy>
            <toy:SearchType>
               <gen:Shop Code="WALMART"/>
            </toy:SearchType>    
         </toy:SearchToy>
         <toy:SearchToy>
            <toy:SearchType>
               <gen:Shop Code="AMAZON"/>
            </toy:SearchType>    
         </toy:SearchToy>
      </toy:SearchReq>
   </soapenv:Body>
</soapenv:Envelope>';

$doc = simplexml_load_string($response);

$path = array($doc
    ->children('http://schemas.xmlsoap.org/soap/envelope/')
    ->Body
    ->children('www.something.com/toy'));


foreach($path as $elem){
    print_r($elem);
    $elemChild = $elem->children('www.something.com/gen');
    var_dump($elemChild);
}

2条回答
霸刀☆藐视天下
2楼-- · 2019-10-03 04:58

You can read SOAP messages by registering the namespace - you only need the namespace bit if you care about the SOAP parts of the message.

$xml = simplexml_load_string($xml);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//toy') as $toy)
{
    print_r($toy);
}
查看更多
干净又极端
3楼-- · 2019-10-03 04:58

See complete reference here: https://stackoverflow.com/a/38783380/6511851

$sxe = new SimpleXMLElement($response); //in $response variable it's an XML file or response
$sxe->registerXPathNamespace('d', 'urn:schemas-microsoft-com:xml-diffgram-v1');
$result = $sxe->xpath("//NewDataSet");

echo "<pre>";
foreach ($result[0] as $title) {
    print_r($title);
}

For more information visit site: https://vaja906programmingworld.wordpress.com/

查看更多
登录 后发表回答