我想从请求的Yahoo YQL结果中返回所有 "name"值,但是我得到的只是一个空页面:(这是我到目前为止的代码:
$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值与名称"名称"?
返回的XML结构示例:sample
非常感谢你的帮助!:)
由于您已经获得了在yahoo yql上查询所需的值,为了获得这些值,由于这是一个查询,因此产生了许多结果。你需要循环它,因为它返回了多个结果。
考虑这个例子:(York作为一个例子。)
$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);
name
的所有值都在$places
内: