简体   繁体   中英

Getting value from SOAP response in php

I am getting a SOAP response for a SOAP call perfectly using SOAP ui but when I call the same in php, I am not able to traverse to the desired element( CreditId in this case), which I want.

The following is the SOAP response that I get using SOAP ui :

<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>

Now I have gone through the various similar questions on the site, where it is advised to do like this to get the desired element :

$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);
    }

However I get an error stating that

Warning: simplexml_load_string() expects parameter 1 to be string

I have tried converting the $res variable to a string but it gives an error that

Object of class stdClass could not be converted to string

But if I do var_dump($res) , I get output like this :

object(stdClass)[2]
  public 'EUserGuid' => string 'ß×!Ì~ßñ±X�PV‚WX' (length=16)
  public 'EtCurrCreditList' => 
    object(stdClass)[3]
  public 'EtErrorLogInfo' => 
    object(stdClass)[4]

Why is the code not going to the sub nodes of the EtCurrCreditList so that I can then process it to get the desired value. - Solved

Final Output :

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
        (
        )

use echo"<pre>";print_r($res); you will be better able to understand how to traverse or show us the result.

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

Now use

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

same for EtCurrCreditList and EtErrorLogInfo and also nested loops if these object contain anything.

updating according to your new output. you can iterate it like this

$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;
}

and can ignore EtErrorLogInfo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM