3

Hello I am getting xml feed from expedia and trying to parse it onto the screen. It is not returning the data, and I can not figure out why. the only thing that will return is activePropertyCount. Here is the url where I am doing this if you want to take a look: http://travellinginmexico.com/test/index.php and here is the xml that I am trying to parse: http://travellinginmexico.com/test/data.xml

My code:

<?php 
$post_string = 'type=xml&cid=55505&minorRev=5&apiKey=4sr8d8bsn75tpcuja6ypx5g3&locale=en_US&currencyCode=USD&customerIpAddress=10.184.2.9&customerUserAgent=Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/535.11+(KHTML,+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 = "http://api.ean.com/ean-services/rs/hotel/v3/list"; //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):
        $title=$info->HotelList;
        $add=$info->address1;
        $count=$info['activePropertyCount'];
        echo "<li><div class=\"title\">",$title,"</div><div class=\"artist\">by ",$add,"</div><time>",$count,"</time></li>\n";
    endforeach;
    echo "</ul>";

?>
luv2code
  • 1,216
  • 6
  • 22
  • 42
  • 1
    Have you verified that you're actually getting the XML string in `$val` before proceeding to try to parse it? – MrCode Mar 16 '12 at 18:18
  • Expedia XML related: [How to fix 411 Length Required error with file_get_contents and the expedia XML API?](http://stackoverflow.com/q/9412650/367456). Better check response codes, bytes written etc. See as well: [Remotely download a file from an external link to my server - download stops prematurely](http://stackoverflow.com/q/9730285/367456). – hakre Mar 16 '12 at 18:23

2 Answers2

1

It appears that you are not trying to retrieve your data from the correct part of the XML object. I have updated your code to read from the HotelList->HotelSummary section, and it will now display the hotel name and address properly.

//Use Curl to get XML here.

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

//Uncomment this to view the parsed XML on screen.
/*
echo '<pre>';
print_r($data->HotelList);
echo '</pre>';
*/

echo "<ul id=\"data\">\n";
foreach($data->HotelList->HotelSummary as $info):
    $title=$info->name;
    $add=$info->address1;
    $count=$data->HotelList['activePropertyCount'];
    echo "<li><div class=\"title\">",$title,"</div><div class=\"artist\">by ",$add,"</div><time>",$count,"</time></li>\n";
endforeach;
echo "</ul>";
Jordan Mack
  • 8,223
  • 7
  • 30
  • 29
0

I recommend checking out libxml_get_errors. There's a fairly good example on how to use it in the PHP documentation for the function. It should be able to tell you what problems are occurring during simplexml_load_file.

If you think the problem is coming from cURL (or to be a little more thorough) you can use the cURL error functions as well. (Such as curl_error).

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131