我有一个保存地址的 mysql 记录数据库。我想迭代这些地址并获取每个地址的地理编码 lat 和 lng 并将它们放入数据库中。我正在使用 php。但是当我运行此代码时,我收到一条错误消息“FastCGI 进程超出了配置的请求超时”。有没有人建议如何让这个程序更好地工作,或者如果没有,其他方法是否会更好?
include_once ('constants_test.php');
// connect to database
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //open db conn
if (mysqli_connect_errno()) {
printf("Connect failed: %s", mysqli_connect_error());
exit();
}
function lookup($string){
ini_set('max_execution_time', 300);
$string = str_replace (" ", "+", urlencode($string));
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$string."&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
// If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST
if ($response['status'] != 'OK') {
return null;
}
//print_r($response);
$geometry = $response['results'][0]['geometry'];
$longitude = $geometry['location']['lat'];
$latitude = $geometry['location']['lng'];
$array = array(
'latitude' => $geometry['location']['lng'],
'longitude' => $geometry['location']['lat'],
);
return $array;
}
$q = "SELECT ShotLocation FROM shotsfired";
$result = $mysqli->query($q);
$count = 0;
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
if ($count < 3) {
$location = $row["ShotLocation"];
$location .= ", Pittsburgh, PA";
$array = lookup($location);
print_r($array);
print_r ("<br/><br/>");
}
$count = $count++;
}
最佳答案
您的请求很可能需要比进程运行的最大允许时间更长的时间。要解决此问题,请将 requestTimeout
的限制提高到更高。
要修改 IIS 网络服务器中的 requestTimeout
,您可以使用 IIS 管理器。转到服务器->IIS->FastCGI 设置/编辑。
或者,这样做。将以下内容添加到您的网络配置中。
<system.web>
<httpRuntime executionTimeout="600" />
</system.web>
(600 秒 = 10 分钟)
关于php - 获取数百个地址的 lat lng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34229531/