2012-01-01 58 views
11

我想使用SimpleXML加載遠程URL。使用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 

是錯誤的最顯着的部分是:分析器錯誤:開始標記預期,「<」未找到

我想訪問該文件,並使用PHP/SimpleXML來格式化結果 - 但到目前爲止,我被卡住了。

我認爲這個線程HERE談論rawurlencode可能有幫助,但它沒有,或者說,或者我失去了一些東西。

回答

30

結果以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); 
?> 
+0

感謝,似乎返回數組,但你能告訴我是什麼有關的URL,它使JSON,因爲它看起來像XML我還什麼將是一個例子處理從上面返回的數據;即echo $ xml-> hotelId [1];或類似的東西?謝謝你的幫助。 – Wordpressing 2012-01-02 04:53:58

+1

網址沒有指定編碼 - 它的Accept標頭的劑量。你的例子幾乎在正確的trac上,但是你應該爲$ XML對象部分使用一個單獨的問題。請不要忘記接受! – Mbrevda 2012-01-04 07:32:30

+0

好的,現在對我更有意義,謝謝你的幫助! – Wordpressing 2012-01-25 05:45:52