我正在使用 SOAP ui 完美地获得 SOAP 调用的 SOAP 响应,但是当我在 php 中调用它时,我无法遍历到我想要的所需元素(在本例中为CreditId )。
以下是我使用 SOAP ui 得到的 SOAP 响应:
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header/>
<soap-env:Body>
<n0:getProjectCreditListResponse xmlns:n0="urn:sap-com:document:sap:soap:functions:mc-style">
<EUserGuid>33/XIcx+3/GxWABQVoJXWA==</EUserGuid>
<EtCurrCreditList>
<item>
<PhaseId/>
<CreditcategoryDescrption>Project Information Forms</CreditcategoryDescrption>
<CreditId>CSD1GSP1L-1000008140</CreditId>
</item>
<item>
<PhaseId/>
<CreditcategoryDescrption>Project Information Forms</CreditcategoryDescrption>
<CreditId>CSD1GSP2L-1000008140</CreditId>
</item>
</EtCurrCreditList>
<EtErrorLogInfo/>
</n0:getProjectCreditListResponse>
</soap-env:Body>
</soap-env:Envelope>
现在我已经浏览了网站上的各种类似问题,建议这样做以获得所需的元素:
$client = new SoapClient('wsdl file path',array('trace'=>1,'exceptions'=>1);
$res = $client->getCreditFormDataXml(array(input arguments));
$xml = simplexml_load_string($res);
$xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('n0', 'urn:sap-com:document:sap:soap:functions:mc-style');
foreach ($xml->xpath('//EtCurrCreditList//item//CreditId') as $item)
{
var_dump($item);
}
但是我收到一条错误消息,指出
警告:simplexml_load_string() 期望参数 1 为字符串
我尝试将$res变量转换为字符串,但它给出了一个错误
类 stdClass 的对象无法转换为字符串
但是如果我做var_dump($res),我会得到这样的输出:
object(stdClass)[2]
public 'EUserGuid' => string 'ß×!Ì~ßñ±X�PV‚WX' (length=16)
public 'EtCurrCreditList' =>
object(stdClass)[3]
public 'EtErrorLogInfo' =>
object(stdClass)[4]
为什么代码不去 EtCurrCreditList 的子节点,以便我可以处理它以获得所需的值。- 解决了
最终输出:
stdClass Object
(
[EUserGuid] => ß×!Ì~ßñ±XPV‚WX
[EtCurrCreditList] => stdClass Object
(
[item] => Array
(
[0] => stdClass Object
(
[PhaseId] =>
[PhaseDescription] =>
[CreditcategoryId] => CSD1GSL-1000008140
[CreditcategoryDescrption] => Project Information Forms
[CreditId] => CSD1GSP1L-1000008140
)
[1] => stdClass Object
(
[PhaseId] =>
[PhaseDescription] =>
[CreditcategoryId] => CSD1GSL-1000008140
[CreditcategoryDescrption] => Project Information Forms
[CreditId] => CSD1GSP2L-1000008140
)
[2] => stdClass Object
(
[PhaseId] =>
[PhaseDescription] =>
[CreditcategoryId] => CSD1GSL-1000008140
[CreditcategoryDescrption] => Project Information Forms
[CreditId] => CSD1GSP3L-1000008140
)
)
)
[EtErrorLogInfo] => stdClass Object
(
)