Link to home
Start Free TrialLog in
Avatar of JF0
JF0Flag for United States of America

asked on

Iterate array from soap call

Hello,

I have virtually no knowledge of PHP so please excuse my lack of knowledge. I am not a programmer for a reason but have to complete a few tasks. A customer of mine provided the following code to make a SOAP call to an API for a transportation management system. The code is as follows:

 
<?php
$soapClient = new SoapClient("https://book.mylimobiz.com/api/ApiService.asmx?wsdl");
   
        // Prepare SoapHeader parameters
        $sh_param = array(
                    'apiId'    =>    '3871fJEy5VUKQQa',
                    'apiKey'    =>    'ZgBY$kPHCUr4W6Z7RxEm');
        $headers = new SoapHeader('https://book.mylimobiz.com/api/ApiService.asmx?wsdl', 'GetVehicleTypes', $sh_param);
   
        // Prepare Soap Client
		$trans = $soapClient->GetVehicleTypes(array('apiId' => '3871fJEy5VUKQQa', 'apiKey' => 'ZgBY$kPHCUr4W6Z7RxEm'))->GetVehicleTypesResult;
		
		// print_r($trans);
				
?>

Open in new window


I don't know how efficient this is, or if there is a much better way to do it (it would be appreciated if you tell me.) All I did was input the apiID and apiKey into the code.Why it is in 2 places, I have no clue...but I can see the output when I uncomment the print_r. The output returned is:

 
stdClass Object
(
    [ResponseCode] => 0
    [ResponseText] => OK
    [VehicleTypes] => stdClass Object
        (
            [VehicleType] => Array
                (
                    [0] => stdClass Object
                        (
                            [VehTypeCode] => LIMO8
                            [VehTypeTitle] => 8-Pax Stretch Limo
                        )

                    [1] => stdClass Object
                        (
                            [VehTypeCode] => LIMOS
                            [VehTypeTitle] => SUV Stretch Limo
                        )

                    [2] => stdClass Object
                        (
                            [VehTypeCode] => BUSMN
                            [VehTypeTitle] => Mini Bus
                        )

                    [3] => stdClass Object
                        (
                            [VehTypeCode] => BUSL
                            [VehTypeTitle] => Limo Bus
                        )

                    [4] => stdClass Object
                        (
                            [VehTypeCode] => SUV
                            [VehTypeTitle] => SUV
                        )

                    [5] => stdClass Object
                        (
                            [VehTypeCode] => VAN
                            [VehTypeTitle] => Mini Van
                        )

                    [6] => stdClass Object
                        (
                            [VehTypeCode] => 001
                            [VehTypeTitle] => Black SUV
                        )

                    [7] => stdClass Object
                        (
                            [VehTypeCode] => SED-OZ
                            [VehTypeTitle] => Holden Statesman
                        )

                    [8] => stdClass Object
                        (
                            [VehTypeCode] => POR
                            [VehTypeTitle] => stretch limo
                        )

                    [9] => stdClass Object
                        (
                            [VehTypeCode] => MER
                            [VehTypeTitle] => Mercedes Benz GL 450 Stretch
                        )

                    [10] => stdClass Object
                        (
                            [VehTypeCode] => PO
                            [VehTypeTitle] => porsche cayenne
                        )

                    [11] => stdClass Object
                        (
                            [VehTypeCode] => TC
                            [VehTypeTitle] => Lincoln Town Car 3-4 Passenger
                        )

                    [12] => stdClass Object
                        (
                            [VehTypeCode] => SEDAN
                            [VehTypeTitle] => luxury Lincoln Towncar
                        )

                    [13] => stdClass Object
                        (
                            [VehTypeCode] => MERC
                            [VehTypeTitle] => Mercedes S-Class
                        )

                    [14] => stdClass Object
                        (
                            [VehTypeCode] => 8 PAX LIMOUSINE
                            [VehTypeTitle] => 8 PASSENGER LIMOUSINE
                        )

                    [15] => stdClass Object
                        (
                            [VehTypeCode] => SHTVAN
                            [VehTypeTitle] => Outlet Shuttle Van (outside of Palm Springs)
                        )

                    [16] => stdClass Object
                        (
                            [VehTypeCode] => 40/44SEATER(T)
                            [VehTypeTitle] => Outlet shuttle Premium Luxury Sedan 2 Transfer
                        )

                    [17] => stdClass Object
                        (
                            [VehTypeCode] => LOVLIF5
                            [VehTypeTitle] => Black Krystal
                        )

                    [18] => stdClass Object
                        (
                            [VehTypeCode] => CUSTCAR
                            [VehTypeTitle] => Customer&#39;s Vehicle
                        )

                    [19] => stdClass Object
                        (
                            [VehTypeCode] => *S*
                            [VehTypeTitle] => SHUTTLE
                        )

                    [20] => stdClass Object
                        (
                            [VehTypeCode] => SEDAN 01
                            [VehTypeTitle] => Town Car Sedan
                        )

                    [21] => stdClass Object
                        (
                            [VehTypeCode] => TESTVEHICLE
                            [VehTypeTitle] => TESTVEHICLE
                        )

                    [22] => stdClass Object
                        (
                            [VehTypeCode] => TESTVEHICLE2
                            [VehTypeTitle] => TESTVEHICLE2
                        )

                    [23] => stdClass Object
                        (
                            [VehTypeCode] => TESTVEHICLE3
                            [VehTypeTitle] => TESTVEHICLE3
                        )

                )

        )

)

Open in new window


What I am trying to do.. I need to iterate the results into a drop down list in a form where each option looks something like:

<option value="VehTypeCode">VehTypeTitle</option>

Point value high because I need to complete this asap.
Avatar of Derokorian
Derokorian
Flag of United States of America image

This does what you are looking for:
<?php
$soapClient = new SoapClient("https://book.mylimobiz.com/api/ApiService.asmx?wsdl");
   
        // Prepare SoapHeader parameters
        $sh_param = array(
                    'apiId'    =>    '3871fJEy5VUKQQa',
                    'apiKey'    =>    'ZgBY$kPHCUr4W6Z7RxEm');
        $headers = new SoapHeader('https://book.mylimobiz.com/api/ApiService.asmx?wsdl', 'GetVehicleTypes', $sh_param);
   
        // Prepare Soap Client
		$trans = $soapClient->GetVehicleTypes(array('apiId' => '3871fJEy5VUKQQa', 'apiKey' => 'ZgBY$kPHCUr4W6Z7RxEm'))->GetVehicleTypesResult;
		
		// print_r($trans);
      $arr = $trans->VehicleTypes->VehicleType;
      echo '<select name="VehicleType">';
      foreach( $arr as $v ) {
         echo '<option value="'.$v->VehTypeCode.'">'.$v->VehTypeTitle.'</option>'."\n";
      }
      echo '</select>';

?>

Open in new window

Avatar of JF0

ASKER

Great, that does seem to create desired output. I will award the points. However, I have one quick question...

The actual apiID and apiKey are entered twice in this code. Will it work if I wanted to pull the apiID and apiKey from a global variable running in our site? So, instead of the strings for apiId and apiKey could I just use $globalApiId or $globalApiKey ?
ASKER CERTIFIED SOLUTION
Avatar of Derokorian
Derokorian
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Agree with Derokorian about defining constants.  You might put these into a "common.php" file that you include() on every page load.  

Going forward you might want to ask yourself this question: Does this project have any economic value at all?  If the answer is "yes" consider hiring a professional programmer.  You can get answers to your questions here at EE.  What you cannot get is an understanding of the principles of computer science or data security.  As the great fire fighter Red Adair said, "If you think it's expensive to hire a professional, just wait till you hire an amateur!"
Avatar of JF0

ASKER

Thanks guys. Appreciate the help.

I will def. be hiring someone to do this for us in the future. It was a time sensitive project that was kind of thrown at me.