Php does not read xml data

Hello I am getting an XML stream from expedia and am trying to parse it on screen. It doesn't return data and I can't figure out why. the only thing that will be returned is activePropertyCount. Here is the URL where I am doing this if you want to take a look: and here is the xml I am trying to parse:

My code:

$post_string = 'type=xml&cid=55505&minorRev=5&apiKey=4sr8d8bsn75tpcuja6ypx5g3&locale=en_US&currencyCode=USD&customerIpAddress=,+like+Gecko)+Chrome/17.0.963.79+Safari/535.11&customerSessionId=&xml=<HotelListRequest><arrivalDate>04/05/2012</arrivalDate><departureDate>04/07/2012</departureDate><RoomGroup><Room><numberOfAdults>2</numberOfAdults></Room><Room><numberOfAdults>2</numberOfAdults><numberOfChildren></numberOfChildren></Room></RoomGroup><city>Cancun</city><countryCode>MX</countryCode><supplierCacheTolerance>MED_ENHANCED</supplierCacheTolerance></HotelListRequest> ';
$path = ""; //Relative path to the file with $_POST parsing
$ch = curl_init($path); 
$fp = fopen('data.xml','w');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); //Send the data to the file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml')); 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FILE, $fp);
$val = curl_exec($ch);
curl_close($ch);//Close curl session
fclose($fp); //Close file overwrite

 $data = simplexml_load_file('data.xml');
    echo "<ul id=\"data\">\n";
    foreach ($data as $info):
        echo "<li><div class=\"title\">",$title,"</div><div class=\"artist\">by ",$add,"</div><time>",$count,"</time></li>\n";
    echo "</ul>";




source to share

2 answers

It looks like you are not trying to get data from the correct part of the XML object. I updated your code to read from the HotelList-> HotelSummary section and it will now display the hotel name and address correctly.

//Use Curl to get XML here.

$data = simplexml_load_file('data.xml');

//Uncomment this to view the parsed XML on screen.
echo '<pre>';
echo '</pre>';

echo "<ul id=\"data\">\n";
foreach($data->HotelList->HotelSummary as $info):
    echo "<li><div class=\"title\">",$title,"</div><div class=\"artist\">by ",$add,"</div><time>",$count,"</time></li>\n";
echo "</ul>";




I recommend checking libxml_get_errors

. There's a pretty good example of how to use it in the PHP documentation for this function. He should be able to tell you what problems arise during simplexml_load_file


If you think the problem is coming from cURL (or being a little more thorough), you can also use cURL's error functions. (For example curl_error




All Articles