我得到了预期的肥皂响应,然后转换为数组。这是我的代码:
$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);
这里是输出:
Array (
[GetCompanyCodeResponse] => Array (
[GetCompanyCodeResult] => Array (
[Customers] => Array (
[Customer] => Array (
[attributes] => Array (
[CustomerNo] => 103987
[CustomerName] => epds api testers Inc
[ContactId] => 219196
)
)
)
)
)
我如何回显ContactId?香港专业教育学院尝试以下:
$att = $array->attributes();
$array->attributes()->{'ContactId'};
print_r($array);
我收到以下错误:
Fatal error: Uncaught Error: Call to a member function attributes() on array
也尝试过:
$array->Customer['CustomerId'];
我收到以下错误:
Notice: Trying to get property 'Customer' of non-object
期望得到219196
我找到了上述问题的解决方案。不确定这是否是最优雅的方法,但是它会返回预期的结果。如果有一种更有效的方法来获取ContactId,我欢迎提出建议。
print_r($array['GetCompanyCodeResponse']['GetCompanyCodeResult']
['Customers']['Customer']['attributes']['ContactId']);