使用SimpleXML加载远程URL

时间:2012-01-01 12:17:06

标签: php xml simplexml

我正在尝试使用SimpleXML加载远程URL。

如果我在浏览器中输入以下内容;

http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US&currencyCode=USD&customerIpAddress=10.184.2.9&customerUserAgent=Mozilla/5.0+(Windows;+U;+Windows+NT+6.1;+en-US;+rv:1.9.2.25)+Gecko/20111212+Firefox/3.6.25&customerSessionId=&xml=<HotelListRequest><arrivalDate>01/22/2012</arrivalDate><departureDate>01/24/2012</departureDate><RoomGroup><Room><numberOfAdults>1</numberOfAdults><numberOfChildren>1</numberOfChildren><childAges>4</childAges></Room></RoomGroup><city>Amsterdam</city><countryCode>NL</countryCode><supplierCacheTolerance>MED</supplierCacheTolerance></HotelListRequest> 

有效!

如果我尝试;

$url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US&currencyCode=USD&customerIpAddress=10.184.2.9&customerUserAgent=Mozilla/5.0+(Windows;+U;+Windows+NT+6.1;+en-US;+rv:1.9.2.25)+Gecko/20111212+Firefox/3.6.25&customerSessionId=&xml=<HotelListRequest><arrivalDate>01/22/2012</arrivalDate><departureDate>01/24/2012</departureDate><RoomGroup><Room><numberOfAdults>1</numberOfAdults><numberOfChildren>1</numberOfChildren><childAges>4</childAges></Room></RoomGroup><city>Amsterdam</city><countryCode>NL</countryCode><supplierCacheTolerance>MED</supplierCacheTolerance></HotelListRequest> ';
$xml = simplexml_load_file($url);

我遇到以下错误;

Warning: simplexml_load_file() [function.simplexml-load-file]: http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US&currencyCode=USD&customerIpAddress=10.184.2.9&customerUserAgent=Mozilla/5.0%2B(Windows;%2BU;%2BWindows%2BNT%2B6.1;%2Ben-US;%2Brv:1.9.2.25)%2BGecko/20111212%2BFirefox/3.6.25&customerSessionId=&xml=%3CHotelListRequest%3E%3CarrivalDate%3E01/22/2012%3C/arrivalDate%3E%3CdepartureDate%3E01/24/2012%3C/departureDate%3E%3CRoomGroup%3E%3CRoom%3E%3CnumberOfAdults%3E1%3C/numberOfAdults%3E%3CnumberOfChildren%3E1%3C/numberOfChildren%3E%3CchildAges%3E4%3C/childAges%3E%3C/Room%3E%3C/RoomGroup%3E%3Ccity%3EAmsterdam%3C/city%3E%3CcountryCode%3ENL%3C/countryCode%3E%3CsupplierCacheTolerance%3EMED%3C/supplierCacheTolerance%3E%3C/HotelListRequest%3E%20:1: parser error : Start tag expected, '<' not found in C:\Program Files\XAMPP\xampplite\htdocs\hotel\results.php on line 29

该错误最值得注意的部分是:解析器错误:期望开始标记,'&lt;'找不到

我想访问该文件并使用PHP / SimpleXML格式化结果 - 但到目前为止我被卡住了。

我认为讨论rawurlencode的这个帖子HERE可能有所帮助,但它没有,或者我错过了什么。

1 个答案:

答案 0 :(得分:32)

结果以json的形式回归。将simplexml_load_file替换为json_decode,您将看到一个合适的对象。

如果要使用xml,则需要在标题中指定它。以下代码将返回有效的xml:

$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US&currencyCode=USD&customerIpAddress=10.184.2.9&customerSessionId=&xml=<HotelListRequest><arrivalDate>01/22/2012</arrivalDate><departureDate>01/24/2012</departureDate><RoomGroup><Room><numberOfAdults>1</numberOfAdults><numberOfChildren>1</numberOfChildren><childAges>4</childAges></Room></RoomGroup><city>Amsterdam</city><countryCode>NL</countryCode><supplierCacheTolerance>MED</supplierCacheTolerance></HotelListRequest> ';

$xml = file_get_contents($url, false, $context);
$xml = simplexml_load_string($xml);
print_r($xml);
?>