在过去的几天里,我一直在绞尽脑汁思考以下问题:我使用以下代码向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,我可以想象返回的对象是空的,因为不包括名称空间。
任何帮助和建议都是非常感激的!- Stefan
由于响应包含多个名称空间,因此需要使用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;
}