我正在尝试更新mysqli数据库中的字段。我正在尝试使用googlemaps api将lat / lng地理编码填入数据库。我知道googlemaps有一个使用限制,但我在Google开发者控制台中创建了一个项目,并制作了一个API密钥。但我没有做的工作 - 我仍然得到OVER_QUERY_LIMIT错误。 (我想,在试图解决我的代码时,我超过了极限。)
这是一个失败的原因吗?我是否必须手动查找每个地址的纬度/经度?如何获取每个(400多个)地址的googlemap lat / lng?
这是我的php代码:
include_once ('constants_test.php');
// connect to database
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s", mysqli_connect_error());
exit();
}
date_default_timezone_set('America/New_York');
$q = "SELECT provider_ID, location FROM `data` where point = ''";
$result = $mysqli->query($q);
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$location = $row['location'];
$provider = $row['provider_ID'];
set_time_limit ( 120 );
geocode($location, $provider);
}
function geocode($address, $provider){
// url encode the address
$address = urlencode($address);
// google map geocode api url
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}&sensor=false";
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
echo $resp['status'];
// response status will be 'OK', if able to geocode given address
if($resp['status']=='OK'){
// get the important data
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
$formatted_address = $resp['results'][0]['formatted_address'];
// verify if data is complete
if($lati && $longi && $formatted_address){
$new_location = "(" . $lati . "," . $longi . ")";
$mysqli2 = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //open db conn
$new_q = "Update `data` set point = '" . $new_location . "' where provider_ID = '" . $provider . "'";
echo $new_q;
$result2 = $mysqli2->query($new_q);
}else{
return false;
}
}else{
return false;
}
}