Marty
Marty

Reputation: 4657

formatting a Complex PHP Array returned by SOAP Call

I'm currently working with a webserivce using nusoap to build my method calls, my problem is the array which is returned and how to format this in a manner which is useable, other projects I've worked on returns s much simpler structured xml file, were i could use $xml = simplexml_load_string($xmlfile); to build and sort my arrays,

this is the nusoap call

$PickUpDateTime='2011-03-17T09:00:00';
$ReturnDateTime='2011-03-21T09:00:00';
require_once('nusoap/lib/nusoap.php');
$wsdl="Srvc.asmx?WSDL";
$client = new nusoap_client($wsdl, 'wsdl'); 
$param = array('OTA_VehAvailRateRQ' => 
        array('VehAvailRQCore' => 
           array('VehRentalCore' => 
            array('PickUpDateTime'=>$PickUpDateTime,
                'ReturnDateTime'=>$ReturnDateTime,
                'PickUpLocation' => array('LocationCode' =>$location),
                'ReturnLocation' => array('LocationCode' =>$location)
            )//PickUpDateTime, ReturnDateTime, PickUpLocation, ReturnLocation
               )//VehRentalCore
        )//VehAvailRQCore
        ); //OTA_VehAvailRateRQ

// LOAD OUR VARIABLE WITH THE RETURN ARRAY
$res = $client->call('GetVehAvailRate', $param);

nomally at this point I would use the simplexml_load_string(); but this dosnt seem to work with this call, doing a dump of $res produces.. ( Lots of data is missing due to the posting limit... but once the array gets to the Vehicle theres a lor more arrays in there...

    Array
(
    [Success] => 
    [VehAvailRSCore] => Array
        (
            [VehRentalCore] => Array
                (
                    [PickUpLocation] => Array
                        (
                            [!LocationCode] => LOCATIONNAME
                        )

                    [ReturnLocation] => Array
                        (
                            [!LocationCode] => LOCATIONNAME
                        )

                    [!PickUpDateTime] => 2011-03-23T09:00:00
                    [!ReturnDateTime] => 2011-03-31T09:00:00
                )

            [VehVendorAvails] => Array
                (
                    [VehVendorAvail] => Array
                        (
                            [Vendor] => Array
                                (
                                    [!Code] => AD
                                )

                            [VehAvails] => Array
                                (
                                    [VehAvail] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [VehAvailCore] => Array
                                                        (
                                                            [Vehicle] => Array ()
                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [VehAvailCore] => Array
                                                        (
                                                            [Vehicle] => Array ()
                                                        )
                                                )

                                            [2] => Array
                                                (
                                                    [VehAvailCore] => Array
                                                        (
                                                            [Vehicle] => Array()
                                                        )
                                                )

                                            [3] => Array
                                                (
                                                    [VehAvailCore] => Array
                                                        (
                                                            [Vehicle] => Array()

                                                        )
                                        )

                                )

                            [Info] => Array
                                (
                                    [LocationDetails] => 
                                )

                        )

                )

        )

    [!EchoToken] => xxxxxxxx
    [!TimeStamp] => 2011-03-18T11:35:19.165125-04:00
    [!Target] => xxxxx
    [!Version] => xxxxxx
    [!TransactionIdentifier] => xxx
)

When trying to loop through the multiarray using a foreach loop is proving a little more harder than first thought...

now when Im trying to access each element using a foreach loop, I'm having to use the literal calls, ie: $res['VehAvailRSCore']; also those exclamation marks i havent came across before either: ie !LocationCode, normaly this would be just $res->LocationCode

So my question is this... how can i use this array to my advantage? just so i can call each array element, and possibly add this / these values to an other array so i can format my page better?

thanks

Upvotes: 0

Views: 1938

Answers (2)

Marty
Marty

Reputation: 4657

Just wanted to post which code has worked for me.. I've changed out the call to the nusoap class, and replaced it with PHP SoapClass

$wsdl="..Srvc.asmx?WSDL";
$client = new SoapClient($wsdl); 
$param = array('OTA_VehAvailRateRQ' => 
           array('VehAvailRQCore' =>
             array('VehRentalCore' => 
               array('PickUpDateTime'=>$PickUpDateTime,
                     'ReturnDateTime'=>$ReturnDateTime,
                     'PickUpLocation' => array('LocationCode' =>$location),
                     'ReturnLocation' => array('LocationCode' =>$location)
                    )
             )//VehRentalCore
           )//VehAvailRQCore
         ); //OTA_VehAvailRateRQ
$res = $client->__soapCall('GetVehAvailRate', $param);

The return is now an Object, which wont display those horrid exclamation marks, to access each value, i used the non literal way,

foreach($res->subkey->subsubkey->subsubsubkey->subsubsubsubkey as $result) {
   echo "<div style='padding:10px; border:1px solid #ccc;'>";
   foreach($result as $value){
      $string = $value->subkey->subsubkey->subsubsubkey;
      echo "<pre>";
      print_r($value);
      echo "</pre>";
   }
   echo "</div>";
}

Hope someone else can get use from it.. Thanks for your help :)

Upvotes: 1

cweiske
cweiske

Reputation: 31078

Dude, nusoap is dead. Do not use it. Use PHP's built-in SOAP class instead.

Accessing the array values has to done with

$a['key1']['subkey']['subsubkey']['subsubkey']
  • PHP itself provides no way apart from that.

Upvotes: 1

Related Questions