troubleshooting Question

Parse simplexml_load_string response

Avatar of dovercomputers
dovercomputers asked on
PHPXML
3 Comments1 Solution396 ViewsLast Modified:
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);

to which I have added

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

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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros