Link to home
Start Free TrialLog in
Avatar of dovercomputers
dovercomputers

asked on

Parse simplexml_load_string response

Hello experts.

I am really struggling to get my head around this. I receive an xml response from a php rest api call. Working code is below:

$xmlReq = <<<END
<?xml version="1.0"?>
<OTA_VehLocSearchRQ xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="0" xmlns="http://www.opentravel.org/OTA/2003/05">
  <POS>
    <Source>
      <RequestorID Type="1" ID="D740B0D6-9111-43DE-A3C3-FD923A2FFE9B" />
    </Source>
  </POS>
  <Vendor CompanyShortName="VHN,A4288CA0-AD96-47E8-957D-4963DACA85B3" />
</OTA_VehLocSearchRQ>
END;

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, 'https://broker.vehiclehirenet.com/TestBrokerServer/Broker/OTA/Vehicle/Location/Search');
   curl_setopt ($ch, CURLOPT_POST, 1);
   curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Content-type: text/xml; charset=utf-8"));
   curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt ($ch, CURLOPT_POSTFIELDS, $xmlReq);
   $store = curl_exec ($ch);
   $xmlResponse = simplexml_load_string($store);

Open in new window


to which I have added

foreach ($xmlResponse->VehMatchedLocs->VehMatchedLoc as $value){
   print_r($value);
   echo "<br /><br />";
} 

Open in new window


This gives me a response, see link CLICK HERE

So it is broken down into VehMatchedLoc which is perfect. How do I access the various levels from here? For example If want to build a select box with a list of the City Names how do I access [CityName] ?

If I can get my head around this all the pieces for the rest of the script will fall into place. I can re-use this code for the rest of the calls.

Hope you can help

Thanks
Jim
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
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
Avatar of dovercomputers
dovercomputers

ASKER

Many thanks Ray, top man.

Jim
Thanks for the points, it's a great question. ~Ray