提取雅虎api数据-php?
如何通过php获取质心、位置、国家等信息?提取雅虎api数据-php?,php,curl,yahoo-api,Php,Curl,Yahoo Api,如何通过php获取质心、位置、国家等信息? { "query" : { "count" : 2, "created" : "2014-03-08T10:16:51Z", "lang" : "en-US", "results" : { "place" : [ { "admin1" : { "code" : "IN-DL", "content" : "Delhi", "type" : "Territory",
{ "query" : { "count" : 2,
"created" : "2014-03-08T10:16:51Z",
"lang" : "en-US",
"results" : { "place" : [ { "admin1" : { "code" : "IN-DL",
"content" : "Delhi",
"type" : "Territory",
"woeid" : "20070458"
},
"admin2" : { "code" : "",
"content" : "New Delhi",
"type" : "District",
"woeid" : "28743736"
},
"admin3" : null,
"areaRank" : "4",
"boundingBox" : { "northEast" : { "latitude" : "28.645321",
"longitude" : "77.240784"
},
"southWest" : { "latitude" : "28.569349",
"longitude" : "77.166237"
}
},
"centroid" : { "latitude" : "28.607330",
"longitude" : "77.203506"
},
"country" : { "code" : "IN",
"content" : "India",
"type" : "Country",
"woeid" : "23424848"
},
"lang" : "en-US",
"locality1" : { "content" : "Delhi",
"type" : "Town",
"woeid" : "2295019"
},
"locality2" : null,
"name" : "New Delhi",
"placeTypeName" : { "code" : "9",
"content" : "District"
},
"popRank" : "0",
"postal" : null,
"timezone" : { "content" : "Asia/Kolkata",
"type" : "Time Zone",
"woeid" : "28350818"
},
"uri" : "http://where.yahooapis.com/v1/place/28743736",
"woeid" : "28743736"
},
{ "admin1" : { "code" : "US-IL",
"content" : "Illinois",
"type" : "State",
"woeid" : "2347572"
},
"admin2" : { "code" : "",
"content" : "Jersey",
"type" : "County",
"woeid" : "12588119"
},
"admin3" : null,
"areaRank" : "2",
"boundingBox" : { "northEast" : { "latitude" : "39.043468",
"longitude" : "-90.245506"
},
"southWest" : { "latitude" : "39.025280",
"longitude" : "-90.268921"
}
},
"centroid" : { "latitude" : "39.034382",
"longitude" : "-90.257217"
},
"country" : { "code" : "US",
"content" : "United States",
"type" : "Country",
"woeid" : "23424977"
},
"lang" : "en-US",
"locality1" : { "content" : "New Delhi",
"type" : "Town",
"woeid" : "23510871"
},
"locality2" : null,
"name" : "New Delhi",
"placeTypeName" : { "code" : "7",
"content" : "Town"
},
"popRank" : "1",
"postal" : { "content" : "62052",
"type" : "Zip Code",
"woeid" : "12784947"
},
"timezone" : { "content" : "America/Chicago",
"type" : "Time Zone",
"woeid" : "56043661"
},
"uri" : "http://where.yahooapis.com/v1/place/23510871",
"woeid" : "23510871"
}
] }
} }
它不进入
if(is_array($json)){
循环。您需要使用json\u decode()
:
然后使用var\u dump()
可以查看对象中的内容并选择所需内容:
$json = json_decode(curl_exec($ch)); //RESULT of YQL QUERY
以这个例子为例
试着这样做:
var_dump($json->results);
这是另一种方法,将json转换为数组,然后显示
$json = json_decode($data, true);
//print_r($json);
foreach($json as $query){
foreach($query['results']['place'] as $places){
echo "Latitude: " . $places['centroid']['latitude'] . "\n";
echo "longitude: " . $places['centroid']['longitude'] . "\n\n";
}
}
$data=json\u decode(curl\u exec($ch),true);
//打印(数据);
foreach($data[“query”][“results”][“place”]作为$key=>$val){
如果(数组_键_存在(“质心”$val)){
回声“--------------------------
”;
foreach($val[“形心”]为$k=>$v){
回声$k.::'.$v;
回声“
”;
}
回声“--------------------------
”;
}
}
您可以在print_r()中检查数据是如何显示的,并且可以使用它来显示您想要的任何项目。严重性:警告消息:为foreach()提供的参数无效,请尝试使用$json=curl_exec($ch);然后是$data=json_decode($json,true);我使用您的json字符串进行了测试,效果良好。
var_dump($json->results);
$json = json_decode($data, true);
//print_r($json);
foreach($json as $query){
foreach($query['results']['place'] as $places){
echo "Latitude: " . $places['centroid']['latitude'] . "\n";
echo "longitude: " . $places['centroid']['longitude'] . "\n\n";
}
}
$data = json_decode(curl_exec($ch),true);
//print_r($data);
foreach($data["query"]["results"]["place"] as $key=>$val){
if(array_key_exists("centroid",$val)){
echo "-----------------------<br />";
foreach($val["centroid"] as $k=>$v){
echo $k.' :: '.$v;
echo '<br />';
}
echo "-----------------------<br />";
}
}