从php中获取SOAP响应的价值

时间:2013-05-22 17:02:18

标签: php soap wsdl soapui soap-client

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

1 个答案:

答案 0 :(得分: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 
       }

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

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

$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