2012-03-16 83 views
3

你好,我正在从expedia获取xml feed并试图将它解析到屏幕上。它没有返回数据,我不知道为什么。唯一会返回的是activePropertyCount。这里是URL,如果你想看看在那里我这样做:http://travellinginmexico.com/test/index.php这里是我试图解析XML:http://travellinginmexico.com/test/data.xmlPhp不读取xml数据

我的代码:

<?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>"; 

?> 
+1

你是否确实已经在'$ val'中得到了XML字符串,然后继续尝试解析它? – MrCode 2012-03-16 18:18:09

+0

与XML相关的XML:[如何修复411 file_get_contents和expedia XML API的长度错误?](http://stackoverflow.com/q/9412650/367456)。更好地检查响应代码,写入字节等。另请参见:[远程将文件从外部链接下载到我的服务器 - 提前下载停止](http://stackoverflow.com/q/9730285/367456)。 – hakre 2012-03-16 18:23:34

回答

1

看来您并未试图从XML对象的正确部分检索数据。我已更新您的代码以从HotelList-> HotelSummary部分读取,并且现在将正确显示酒店名称和地址。

//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>"; 
+0

你真棒,这就像一个魅力。非常感谢! – liveandream 2012-03-16 19:22:23

0

我建议检查出libxml_get_errors 。关于如何在函数的PHP文档中使用它,有一个很好的例子。它应该能够告诉你在simplexml_load_file期间发生了什么问题。

如果您认为问题来自cURL(或者要更彻底一点),那么您也可以使用cURL错误函数。 (如curl_error)。