这只是对较早解决的问题的略微更新。我只是想从2个不同的JSON
结果中获取2个城市名称作为变量。效果很好,但是有时候或有时两者什么都不返回,什么也没有改变?
我做错了什么吗,或者有一种方法可以循环直到得到结果?
对不起,这很痛苦,但我真的看不出为什么它会碰到吗?
PHP
中运行两个查询并有时获得结果的工作代码!
$p_lat = "52.406822";
$p_lng = "-1.519693";
$d_lat = "50.87626460000001";
$d_lng = "-0.3717470999999932";
$county = "";
$town = "";
$d_county = "";
$d_town = "";
$result = @file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=".$p_lat.",".$p_lng."&sensor=true" );
if ($result === FALSE) {
//manage exception from file_get_contents call
} else {
$geocodedinfo = json_decode($result);
if ($geocodedinfo->status == "OK") {
$county = "";
$town = "";
foreach ($geocodedinfo->results[0]->address_components as $addrcomps) {
if ( $addrcomps->types[0] == 'postal_town')
$town = $addrcomps->long_name;
}
}
}
$result = @file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=".$d_lat.",".$d_lng."&sensor=true" );
if ($result === FALSE) {
//manage exception from file_get_contents call
} else {
$geocodedinfo = json_decode($result);
if ($geocodedinfo->status == "OK") {
$county = "";
$d_town = "";
foreach ($geocodedinfo->results[0]->address_components as $addrcomps) {
if ( $addrcomps->types[0] == 'postal_town')
$d_town = $addrcomps->long_name;
}
}
}
echo $town;
echo "<br>";
echo $d_town;
答案 0 :(得分:1)
它是固定的。我必须在其中添加HTTPS并包含一个密钥。我没有开始,因为它没有一个就开始工作!!哦,很好。
$result = @file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=".$p_lat.",".$p_lng."&sensor=true&key=KEYHERE&" );
仍然感谢
答案 1 :(得分:1)
当我尝试打印$result
时:
$result = @file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=".$p_lat.",".$p_lng."&sensor=true" );
echo "<pre>";
print_r(json_decode($result));
exit;
以下是响应:
stdClass Object
(
[error_message] => You have exceeded your daily request quota for this API. We recommend registering for a key at the Google Developers Console: https://console.developers.google.com/apis/credentials?project=_
[results] => Array
(
)
[status] => OVER_QUERY_LIMIT
)
因此,请尝试先注册您的应用程序。最终将解决您的问题。