我正在尝试使用SimpleXML加载远程URL。
如果我在浏览器中键入以下内容;
http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US¤cyCode=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¤cyCode=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¤cyCode=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
该错误中最值得注意的部分是:分析器错误:预期开始标记,未找到'
我想访问该文件并使用PHP/SimpleXML格式化结果-但到目前为止,我仍然遇到问题。
我以为这个讨论rawurlencode的HERE线程可能有所帮助,但没有帮助,或者我错过了一些东西。
最佳答案
结果以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¤cyCode=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);
?>
关于php - 使用SimpleXML加载远程URL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8693043/