0

I have SOAP XML response like below, please suggest me how can I find the data like room code, hotel code, currency etc. from this. I also used below code but its not working means it returns empty.

$xmlObject = simplexml_load_string($xmlString);

$array = json_decode(json_encode($xmlObject), true);

The corresponding XML response :

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <ResponseId xmlns="http://www.example.com/">e96c9439-6049-4abb-b070-f7f0024153b7@52-20-187-176</ResponseId>
  </soap:Header>
  <soap:Body>
    <ns4:FetchRoomAllotmentResponse xmlns:ns4="http://www.example.com/" xmlns:ns2="http://www.example.com" xmlns:ns3="http://www.example.com/">
      <ns4:FetchRoomAllotmentResult>
        <ns4:Response>
          <ns4:Hotel Code="201511191642109768" Name="TestCMHotel" />
          <ns4:Rooms>
            <ns4:Room Code="171578" Name="2 Bed Super Deluxe">
              <ns4:Rates>
                <ns4:Rate Currency="INR" Description="2 Bed Super Deluxe" Id="71624608833328" MealPlan="MAP" ValidFrom="2015-11-19" ValidTo="2017-04-30">
                  <ns4:Inclusions>
                    <ns4:Inclusion code="test inclusion test inclusion test inclusion test inclusion test inclusion">test inclusion test inclusion test inclusion test inclusion test inclusion</ns4:Inclusion>
                  </ns4:Inclusions>
                </ns4:Rate>
              </ns4:Rates>
            </ns4:Room>
            <ns4:Room Code="19505" Name="AC Deluxe Room AC">
              <ns4:Rates>
                <ns4:Rate Currency="INR" Description="AC Deluxe Room AC" Id="5835231260301" MealPlan="EP" ValidFrom="2015-11-19" ValidTo="2017-05-31">
                  <ns4:Inclusions>
                    <ns4:Inclusion code="test 1">test 1</ns4:Inclusion>
                    <ns4:Inclusion code="test inclusion test inclusion test inclusion test inclusion test inclusion">test inclusion test inclusion test inclusion test inclusion test inclusion</ns4:Inclusion>
                  </ns4:Inclusions>
                </ns4:Rate>
              </ns4:Rates>
            </ns4:Room>
            <ns4:Room Code="18303" Name="Standar AC">
              <ns4:Rates>
                <ns4:Rate Currency="INR" Description="Standar AC" Id="79045408874412" MealPlan="CP" ValidFrom="2015-11-19" ValidTo="2017-06-30">
                  <ns4:Inclusions>
                    <ns4:Inclusion code="test wifi and swiimming pool on a stay og one night or multiple nights 1 night or multiple nights te">test wifi and swiimming pool on a stay og one night or multiple nights 1 night or multiple nights te</ns4:Inclusion>
                  </ns4:Inclusions>
                </ns4:Rate>
              </ns4:Rates>
            </ns4:Room>
          </ns4:Rooms>
        </ns4:Response>
      </ns4:FetchRoomAllotmentResult>
    </ns4:FetchRoomAllotmentResponse>
  </soap:Body>
</soap:Envelope>
jmgross
  • 2,306
  • 1
  • 20
  • 24

4 Answers4

2

Try this example soap Response to array value get:

if( ! $response = curl_exec($cURL)){<br/>
  trigger_error(curl_error($cURL));<br/>
}

curl_close ( $cURL );<br/><br/>
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);<br/>
$xml = simplexml_load_string($xml);<br/>
$json = json_encode($xml);<br/>
$responseArray = json_decode($json,true);

//Find value 
findKey($responseArray['sBody'],'val');

This function will help for find key & value from array

public static function findKey($array, $keySearch)
{
    foreach ($array as $key => $item) {
        if ($key === $keySearch) {
            return $item;
        }
        else {
            if (is_array($item)) {
                return  self::findKey($item, $keySearch);
            }
        }
    }
    return false;
}
Surendra Suthar
  • 350
  • 2
  • 11
0

use simplexml_load_string() for this it:

Interprets a string of XML into an object

More info here http://php.net/manual/en/function.simplexml-load-string.php

ecorvo
  • 3,559
  • 4
  • 24
  • 35
0

May be this will work for you.

$xml = new SimpleXMLElement($xmlObject);
$xml->registerXPathNamespace('ns4', 'http://www.example.com/');
foreach($xml->xpath('//ns4:Room') as $room) {
    //var_export($room);
    //var_export($room->xpath("//ns4:Inclusion"));
    echo "ROOM Code" . $room["Code"]."<br/>";
    foreach($room->xpath("//ns4:Inclusion") as $inclusion){
        echo "Inclusion Code" .$inclusion["code"]."<br/>";
    }
}

You can read more about this here Parse XML with Namespace using SimpleXML

Community
  • 1
  • 1
angel.bonev
  • 2,154
  • 3
  • 20
  • 30
0

friends, I got the answer. Thank you very much for your comments.

$parser = simplexml_load_string($result);  
$parserEnv = $parser->children('soap', true);
$hotel = $parserEnv->Body->children('ns4', true)
    ->FetchRoomAllotmentResponse->children('ns4', true)
    ->FetchRoomAllotmentResult->children('ns4', true)
    ->Response->children('ns4', true)->Hotel[0]->attributes();

   echo 'Hotel Code= '.$hotel['Code']. '<br>';
   echo 'Hotel Name= '.$hotel['Name']. '<br>';

$room = $parserEnv->Body->children('ns4', true)
    ->FetchRoomAllotmentResponse->children('ns4', true)
    ->FetchRoomAllotmentResult->children('ns4', true)
    ->Response->children('ns4', true)->Rooms->children('ns4', true);

   for($i=0; $i<count($room); $i++)
   {
     $r=$room->Room[$i]->attributes();
     echo 'Room'.$i.' Code='.$r['Code']. '<br>';
     echo 'Room'.$i.' Name='.$r['Name']. '<br>';
    $rate=$room->Room[$i]->children('ns4', true)->Rates->children('ns4', true)->Rate[0]->attributes();
    echo 'Currency= '.$rate['Currency']. '<br>';
   echo 'Description= '.$rate['Description']. '<br>';
   echo 'Id= '.$rate['Id']. '<br>';
   echo 'MealPlan= '.$rate['MealPlan']. '<br>';
   echo 'ValidFrom= '.$rate['ValidFrom']. '<br>';
   echo 'ValidTo= '.$rate['ValidTo']. '<br>';