2014-10-21 2 views
2

XML 형식으로 응답을 보내는 API에 요청을 보내고 있습니다.PHP에서 file_get_contents로 PHP에서 데이터 추출

응답은 다음과 같습니다 : 나는 hotelId 또는 이름과 같은 개별 값을 인쇄 할 수 있도록

<ns2:HotelInformationResponse hotelId="263933"> 
<customerSessionId>0ABAAA85-112B-0914-9322-CA866D907EF8</customerSessionId> 
<HotelSummary order="0"> 
<hotelId>263933</hotelId> 
<name>Nova Platinum Hotel</name> 
<address1>562 Moo 10 Pratamnak Road, Nongprue</address1> 
<address2>Banglamung</address2> 
<city>Pattaya</city> 
<postalCode>20260</postalCode> 
<countryCode>TH</countryCode> 

가 어떻게이 데이터를받을 수 있나요?

는이 같은 시도 :

또한
$Geosearch = 'APICALLURLHERE'; 
$fileContents = file_get_contents($Geosearch); 
$string_data = $fileContents; 
$xml = simplexml_load_string($string_data); 
$hotel_id = (string) $xml->hotelId; 
$hotel_name = (string) $xml->name; 
echo $hotel_id.' '.$hotel_name; 

나는이 시도 :

$xml = simplexml_load_file("APICALLURLHERE"); 
echo $xml->hotelId; 
echo $xml->name; 

전체 응답은 여기에 있습니다 : 원격에

http://paste.ofcode.org/9VAH6jBUPJ4mHaMNkiS44d

+0

을하는 방식의'hotelId'하지'HotelId'에 의해. 그래서 문제가 뭐야? – Ghost

+0

그것은 오타 였지만 어쨌든 어떤 차이도 만들지 않습니다. – user327685

+0

이 완전한 응답입니까? –

답변

0

액세스를 파일

기본적으로 file_get_contents 및 simplexml_load_file을 포함한 fopen 계열의 함수는 원격 파일에 대한 액세스를 허용하지 않습니다. 이것은 아주 좋은 일이며 그렇게해야합니다.

원격 파일을 사용하려면 PHP 구성을 변경해야합니다.

allow_url_fopen = 1 


XML 파일의 오류

http://php.net/manual/en/features.remote-files.php 파일 로딩이 작동하지만, 여전히 XML의 작업을하지 않는 경우, 당신은 깨진 XML 파일이있을 수 있습니다. 이 조각으로 이러한 문제를 디버깅 할 수 있습니다 :

$doc = simplexml_load_string($xmlstr); 
if (!$doc) { 
    $errors = libxml_get_errors(); 

    foreach ($errors as $error) { 
     echo display_xml_error($error, $xml); 
    } 
    libxml_clear_errors(); 
} 

http://php.net/manual/de/function.libxml-get-errors.php

+0

방금'$ url = "APICALLURL"; echo file_get_contents ($ url);'전체 응답이 출력되므로 문제가되지 않을 수 있습니까? – user327685

+0

맞아요. 그러나 마음에 떠오르는 유일한 문제는 깨진 XML 파일뿐입니다. 오류 로그에는 무엇이 있으며 simplexml_load_something()의 응답은 무엇입니까? – ToBe

+0

잠재적 인 XML 오류에 대한 답변이 업데이트되었습니다. – ToBe