2

I'm sending a request to an API which sends a response in XML format.

The response looks like this:

<ns2:HotelInformationResponse hotelId="263933">
<customerSessionId>0ABAAA85-112B-0914-9322-CA866D907EF8</customerSessionId>
<HotelSummary order="0">
<hotelId>263933</hotelId>
<name>Nova Platinum Hotel</name>
<address1>562 Moo 10 Pratamnak Road, Nongprue</address1>
<address2>Banglamung</address2>
<city>Pattaya</city>
<postalCode>20260</postalCode>
<countryCode>TH</countryCode>

How can I get this data so I can print individual values like hotelId or name ?

I have tried like this:

$Geosearch = 'APICALLURLHERE';
$fileContents = file_get_contents($Geosearch);
$string_data = $fileContents;
$xml = simplexml_load_string($string_data);
$hotel_id = (string) $xml->hotelId;
$hotel_name = (string) $xml->name;
echo $hotel_id.' '.$hotel_name;

Also I have tried this:

$xml = simplexml_load_file("APICALLURLHERE");
echo $xml->hotelId;
echo $xml->name; 
Zoe
  • 27,060
  • 21
  • 118
  • 148
user327685
  • 129
  • 9

1 Answers1

0

Acces to remote files

The fopen family of functions, including file_get_contents and simplexml_load_file) by default do no allow access to remote files. This is a very good thing and should stay that way.

If you want to enable remote files, you have to change your PHP configuration.

http://php.net/manual/en/features.remote-files.php

allow_url_fopen = 1


Errors in a XML file

If your file loading is working but you STILL do not get your XML working, you may have a broken XML file. You can debug these issues with this snippet:

$doc = simplexml_load_string($xmlstr);
if (!$doc) {
    $errors = libxml_get_errors();

    foreach ($errors as $error) {
        echo display_xml_error($error, $xml);
    }
    libxml_clear_errors();
}

http://php.net/manual/de/function.libxml-get-errors.php

ToBe
  • 2,667
  • 1
  • 18
  • 30
  • If I just `$url = "APICALLURL"; echo file_get_contents($url);` the whole response gets printed, so this can't be the problem? – user327685 Oct 21 '14 at 13:23
  • You are right, but then the only other problem that come to mind is a broken XML file. What's in your error log and what is the response from simplexml_load_something() ? – ToBe Oct 21 '14 at 13:24
  • Answer updated for potential xml errors – ToBe Oct 21 '14 at 13:26