php - 获取数百个地址的 lat lng

标签 php mysql google-api

我有一个保存地址的 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/

相关文章:

php - 无法在 GET 或 POST 中使用字符串 "execute("

php - 从 MySQL 数据库检索项目,但首先使用一些数学规则

MySQL 连接、分组依据以及按选择排序分组

c# - 使用 API 3.0 获取 YouTube 视频的评论或点赞数

google-maps - API 中是否提供 Gmail "Map this"功能?

php - 公共(public)数组变量未设置 PHP

php - 如何排除所有未通过验证 laravel 的数据?

java - 尝试访问 google api 时 Java 中的 JSON 错误

php - 用户使用 codeigniter 登录后无法将配置文件数据更新到数据库中

mysql - 这是在 Rails 上使用 ruby​​ 的合适时机吗?