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
PHPXML

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
dovercomputers

ASKER
Many thanks Ray, top man.

Jim
Ray Paseur

Thanks for the points, it's a great question. ~Ray
Your help has saved me hundreds of hours of internet surfing.
fblack61