2014-06-16 2 views
0
내가 요청 야후 YQL 결과에서 모든 "이름"값을 반환 할

에서 장소의 이름을 반환하지만, 내가 할 모든 빈 페이지입니다 방법 :(이 지금까지 내 코드입니다 :야후 YQL 결과 XML

내가 구문 분석하고 이름이 "이름"으로 모든 XML 값을 반환 할 수있는 방법
$input = $_GET['str']; 
$yql = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%20='".$input."'"; 

$feed = file_get_contents($yql); 
$xml = simplexml_load_string($feed); 

echo $xml->query->results->place->name; 

반환 된 XML 구조 샘플 : sample

이 도움을 주셔서 대단히 감사합니다 :)

를!

답변

1

yahoo yql에서 쿼리하는 데 필요한 값을 이미 가져 왔으므로이 값이 쿼리이기 때문에 많은 결과가 나타납니다. 여러 결과를 반환했기 때문에 반복해야합니다.

이 예를 고려하십시오. (예를 들어 뉴욕)를 name

$input = 'york'; 
$yql = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%20='".$input."'"; 
$contents = file_get_contents($yql); 
$xml = new SimpleXMLElement($contents); 
$places = array(); 
foreach($xml->results->place as $key => $item) { 
    $country_info = $item->country->attributes(); 
    $places[] = array(
     'placeTypeName' => (string) $item->placeTypeName, 
     'name' => (string) $item->name, 
     'country' => array(
      'code' => (string) $country_info['code'], 
      'type' =>(string) $country_info['type'], 
      'woeid' => (string) $country_info['woeid'], 
     ), 
    ); 
} 

print_r($places); 

모든 값이 $places 내부에 :

Sample Output

+0

대답 주셔서 대단히 감사합니다! :) 마지막으로 질문 : 국가 코드 ( 우루과이 -> "UY")와 위도 및 경도 값을이 방법으로 어떻게 액세스 할 수 있습니까? 중심 부분? json_decode :( – VORiAND

+0

@ VORiAND에 반드시 속성이없는 것은 아닙니다.) 편집 내용을 확인할 수 있습니다. – user1978142