1

我正在使用 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
        (
        )
4

1 回答 1

3

使用echo"<pre>";print_r($res);您将能够更好地理解如何遍历或向我们展示结果。

$res=stdClass Object ( [EUserGuid] => ß×!Ì~ßñ±XPV‚WX [EtCurrCreditList] => stdClass Object ( ) [EtErrorLogInfo] => stdClass Object ( ) )

现在使用

foreach($res->EUserGuid as $data)
       {
         //Your logic 
       }

如果这些对象包含任何内容,则forEtCurrCreditListEtErrorLogInfo嵌套循环也相同。

根据您的新输出进行更新。你可以像这样迭代它

$res->EUserGuid=$something;

foreach($res->EtCurrCreditList->item as $eachItem)
{
    $eachItem->PhaseId=$your_ wish;
    $eachItem->PhaseDescription==$your_ wish;
    $eachItem->CreditcategoryId==$your_ wish;
    $eachItem->CreditcategoryDescrption==$your_ wish;
    $eachItem->CreditId=$your_ wish;
}

并且可以忽略EtErrorLogInfo

于 2013-05-24T10:41:30.373 回答