Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Soap响应PHP获取属性值_Php_Xml_Soap_Simplexml - Fatal编程技术网

从Soap响应PHP获取属性值

从Soap响应PHP获取属性值,php,xml,soap,simplexml,Php,Xml,Soap,Simplexml,我得到了预期的soap响应,然后转换为数组。这是我的密码: $response = $client->__getLastResponse(); $response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response); $xml = new SimpleXMLElement($response); $body = $xml->xpath('//soapBody')[0]; $array = jso

我得到了预期的soap响应,然后转换为数组。这是我的密码:

$response = $client->__getLastResponse();
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//soapBody')[0];
$array = json_decode( str_replace('@', '', json_encode((array)$body)), TRUE); 
print_r($array);
如何回显ContactId?我尝试了以下方法:

$att = $array->attributes();
$array->attributes()->{'ContactId'};
print_r($array);
我得到以下错误:

Fatal error: Uncaught Error: Call to a member function attributes() on array 
Notice: Trying to get property 'Customer' of non-object
还尝试:

$array->Customer['CustomerId'];
我发现以下错误:

Fatal error: Uncaught Error: Call to a member function attributes() on array 
Notice: Trying to get property 'Customer' of non-object

期望得到219196,我找到了上述问题的解决方案。不确定这是否是最优雅的方法,但它会按预期返回结果。如果有更有效的方法获取ContactId,我愿意接受建议

print_r($array['GetCompanyCodeResponse']['GetCompanyCodeResult']
['Customers']['Customer']['attributes']['ContactId']);

您在如何解析XML方面遵循了一些非常糟糕的建议,并完全抛弃了SimpleXML的功能

具体来说,您无法运行
attributes()
方法的原因是,您使用以下难看的方法将SimpleXML对象转换为普通数组:

$array = json_decode( str_replace('@', '', json_encode((array)$body)), TRUE); 
要使用SimpleXML作为其作者,我建议您阅读:

由于您没有在问题中粘贴实际的XML,我猜它看起来是这样的:


如果这是在
$response
中,我们不需要对
str\u replace
json\u encode
做任何奇怪的事情,我们可以使用SimpleXML中内置的方法来浏览XML:

$xml=newsimplexmlement($response);
//主体位于SOAP信封命名空间中
$body=$xml->children('http://www.w3.org/2001/12/soap-envelope“)->身体;
//位于其他命名空间中的元素
$innerResponse=$body->children('http://www.example.org/companyInfo')->GetCompanyCodeResponse;
//我们需要遍历XML以到达我们感兴趣的节点
$customer=$innerResponse->GetCompanyCodeResult->Customers->customer;
//从技术上讲,不固定的属性不在任何名称空间中(XML名称空间规范中的一个奇怪之处!)
$attributes=$customer->attributes(空);
//这是你想要的价值
echo$attributes['ContactId'];
与以前的代码不同,在以下情况下,此代码不会中断:

  • 服务器开始使用不同的本地前缀,而不是
    soap:
    ,或者在
    getcompanycodesponse
    元素上添加前缀
  • 响应返回多个
    客户
    ->Customer
    始终表示与
    ->Customer[0]
    相同,即具有该名称的第一个子元素)
  • Customer
    元素具有子元素或文本内容以及属性

它还允许您使用SimpleXML的其他功能,例如,甚至。

试试这个:
$array['GetCompanyCodeResponse']['GetCompanyCodeResult']['Customers']['attributes']['ContactId']
@Juan.Queiroz这正是我想到的。我尝试使用SimpleXML时,只收到了错误。如果你注意到的话,我说不确定答案是对还是错,但它确实起了作用。谢谢你的意见@jasone那么这个答案对你有帮助吗,或者你还有什么不明白的吗?