simplexml_load_string() will not read soap respons

2020-02-09 11:19发布

I know this may be a newbie question, but please humor me. When reading an xml string with "soap:" in the tags, simplexml_load_string() will not read in the xml.

given this script:

#!/usr/bin/php
<?php

$s='
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
        <soap:Header>
        <context xmlns="urn:zimbra"/>
        </soap:Header>
        <soap:Body>
            <AuthResponse xmlns="urn:zimbraAdmin">
                <authToken>somevalue</authToken>
                <lifetime>123124123</lifetime>
                <a n="zimbraIsDomainAdminAccount">false</a>
            </AuthResponse>
        </soap:Body>
    </soap:Envelope>';


print_r(simplexml_load_string($s));
echo "\n\n";
print_r(simplexml_load_string(str_ireplace("soap:", "", $s)));
?>

I get this output:

jesse@jesse-debian:~/code/zmsoap$ ./xmltest.php 
SimpleXMLElement Object
(
)

SimpleXMLElement Object
(
    [Header] => SimpleXMLElement Object
        (
            [context] => SimpleXMLElement Object
                (
                )

        )

    [Body] => SimpleXMLElement Object
        (
            [AuthResponse] => SimpleXMLElement Object
                (
                    [authToken] => somevalue
                    [lifetime] => 123124123
                    [a] => false
                )

        )

)
jesse@jesse-debian:~/code/zmsoap$

I'm just curious why this is happening, and if there is a more proper way to remedy the problem as opposed to doing a string replace.

3条回答
爷的心禁止访问
2楼-- · 2020-02-09 11:37

This seems to have worked.

Read about it here: Parse XML with Namespace using SimpleXML

#!/usr/bin/php
<?php

$s='
    <soap:Envelope xmlns:soap="urn:zimbra">
        <soap:Header>
        <context xmlns="urn:zimbra"/>
        </soap:Header>
        <soap:Body>
            <AuthResponse xmlns="urn:zimbraAdmin">
                <authToken>somevalue</authToken>
                <lifetime>123124123</lifetime>
                <a n="zimbraIsDomainAdminAccount">false</a>
            </AuthResponse>
        </soap:Body>
    </soap:Envelope>';
//print_r(simplexml_load_string($s));
//echo "\n\n";
//print_r(simplexml_load_string(str_ireplace("soap:", "", $s)));
$xml = simplexml_load_string($s);
$xml->registerXPathNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
print_r($xml->xpath('//soap:Body'));

?>
查看更多
够拽才男人
3楼-- · 2020-02-09 11:55

simplest:

$xml='<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><AddOrderResult>1</AddOrderResult></soap:Body></soap:Envelope>';
var_export(json_decode(json_encode(simplexml_load_string(strtr($xml, array(' xmlns:'=>' ')))), 1));

output:

array (
  '@attributes' => 
  array (
    'soap' => 'http://schemas.xmlsoap.org/soap/envelope/',
  ),
  'soap:Body' => 
  array (
    'AddOrderResult' => '1',
  ),
)
查看更多
冷血范
4楼-- · 2020-02-09 11:57

A tag name with a colon in indicates the tag is in a non-default namespace. SimpleXML only looks at one namespace at a time, so you need to specifically select the namespace using the ->children() method.

In this case $xml->children('http://www.w3.org/2003/05/soap-envelope')->Body or $xml->children('soap', true)->Body should both work.

For this and other reasons, it's not advisable to use print_r to debug SimpleXML objects. Try this dedicated function instead.

查看更多
登录 后发表回答