PHP SOAP 响应包含具有不同 xmlns 的属性并显示为空

标签 php xml web-services soap soap-client

这几天我一直在绞尽脑汁地思考以下问题: 我使用以下代码向 SOAP 服务发送请求:

$client = new SoapClient(WSDL,  array('soap_version'=> SOAP_1_2, 'trace' => 1));

$result = $client->getHumanResourceID(array(
    'cCode' => CLIENT_CODE,
    'hFilter' => array('deltaDatum' => '2010-01-01T00:00:00-00:00')
));

部分 var_dump($result) 显示:

object(stdClass)#2 (1) {
  ["getHumanResourceIDResult"]=>
  object(stdClass)#3 (1) {
    ["EntityIdType"]=>
    array(4999) {
      [0]=>
      object(stdClass)#4 (2) {
        ["IdValue"]=>
        object(stdClass)#5 (0) {
        }
        ["idOwner"]=>
        string(8) "internal"
      }
      [1]=>
      object(stdClass)#6 (2) {
        ["IdValue"]=>
        object(stdClass)#7 (0) {
        }
        ["idOwner"]=>
        string(8) "internal"
      }

EntityType 的对象中发生了一些奇怪的事情,它包含一个具有 idValue 和 idOwner 的对象。 IdValue 应包含其他属性或字段。 此时我不知道要添加或修改什么才能接收这些值。

原始 SOAP 响应的一部分:

<?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>
        <getHumanResourceIDResponse xmlns="http://soapWeb.org/">   
            <getHumanResourceIDResult>
                <EntityIdType idOwner="internal">
                    <IdValue xmlns="http://ns.hr-xml.org/2007-04-15">5429</IdValue>
                </EntityIdType>
            </getHumanResourceIDResult>
        </getHumanResourceIDResponse>
    </soap:Body>
</soap:Envelope>

我注意到的是 IdValue 字段中的 xmlns,我可以想象返回的对象为 null,因为不包含命名空间。

任何帮助和建议都将不胜感激!

  • 斯特凡

最佳答案

由于响应包含多个命名空间,因此需要使用 registerXpathNamespaces (如果使用SimpleXML,对于DOM,也有类似的方法。)读取所有值的函数。

$xml = <<<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>
        <getHumanResourceIDResponse xmlns="http://soapWeb.org/">   
            <getHumanResourceIDResult>
                <EntityIdType idOwner="internal">
                    <IdValue xmlns="http://ns.hr-xml.org/2007-04-15">5429</IdValue>
                </EntityIdType>
            </getHumanResourceIDResult>
        </getHumanResourceIDResponse>
    </soap:Body>
</soap:Envelope>
XML;

$xml = simplexml_load_string( $xml );

$xml->registerXPathNamespace( 's', 'http://soapWeb.org/' );

$xpath = $xml->xpath( '//s:getHumanResourceIDResponse' );

foreach( $xpath as $node ) {
    $idValue = ( string ) $node->getHumanResourceIDResult->EntityIdType->IdValue;
    $idOwner = ( string ) $node->getHumanResourceIDResult->EntityIdType[ 'idOwner' ];

    echo 'IdValue : ' . $idValue . '<br />';
    echo 'IdOwner : ' . $idOwner;
}

希望这有帮助。

关于PHP SOAP 响应包含具有不同 xmlns 的属性并显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23757410/

相关文章:

android - <ConstraintLayout/> 中 <TextView/> 下方的边距不起作用

java - 如何使用 DOM Java 搜索 xml 中的子节点

java - 如何使用 Spring-WS 将异常转换为返回码?

javascript - 使用 PHP 和 javascript 将数据替换为 json 文件

php - 如何在 stdClass 对象数组 PHP 中使用 array_unique 函数

php - "Lost connection to MySQL server"由于各种原因

php - Laravel 虚拟列无法保存

sql - 如何在标签中生成带有非法字符的xml

node.js - 用户提交页面的 Node 堆栈

javascript - CoffeeScript 服务器不如 Node.js 服务器可靠吗?