Parse simplexml_load_string response

dovercomputers
dovercomputers used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
http://www.laprbass.com/RAY_temp_dovercomputers.php
<?php // RAY_temp_dovercomputers.php
error_reporting(E_ALL);

$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);

// ACTIVATE THIS TO SHOW THE OBJECT
// echo "<pre>";
// var_dump($xmlResponse);

// SHOW HOW TO GET SOME OF THE DATA OUT OF THE OBJECT
foreach ($xmlResponse->VehMatchedLocs->VehMatchedLoc as $v)
{
    $n = (string)$v->LocationDetail["Name"];
    $c = (string)$v->LocationDetail->Address->CityName;
    $o = $v->LocationDetail->AdditionalInfo->OperationSchedules->OperationSchedule->OperationTimes->OperationTime[2];
    $s = FALSE;
    if (!empty($o))
    {
        if ($o["Sun"] == "true")
        {
            $s = TRUE;
            $a = $o["Start"];
            $z = $o["End"];
        }
    }

    echo PHP_EOL. '<br/>';
    echo "$n IN $c ";
    if ($s)
    {
        echo "OPERATES ON SUNDAY FROM $a TO $z";
    }
    else
    {
        echo "DOES NOT OPERATE ON SUNDAY";
    }
}

Open in new window

Author

Commented:
Many thanks Ray, top man.

Jim
Most Valuable Expert 2011
Top Expert 2016

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial