1

我是 PHP 新手,我想获取一个地方的纬度和经度,然后将它们添加到 MySQL 数据库中。

我正在使用 Google Geo-code API 来获取它们,这就是我在右边所做的

for ($i = 0; $i<1000; $i++) {
        $sql = mysql_query("SELECT place_address FROM place_locator WHERE place_id =".$i, $this->db) or die('invalide request : ' . mysql_error());

        if (mysql_num_rows($sql)) {
            while ($place = mysql_fetch_assoc($sql)) {

                //Encode the place string I got, to get rid of space
                $encodePlace = str_replace(" ", "%20", $place["place_address"]);
                //Use Google API
                $url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.$encodePlace.'&sensor=false';
                //Use Curl to send the request
                $ch = curl_init($url);

                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                $response = curl_exec($ch);
                $obj = json_decode($response, true);


                $updateSql = mysql_query("UPDATE  `place_locator`.`place_locator` SET
                `latitude` =  '".$obj["results"][0]["geometry"]["location"]["lat"]."',
                `longitude` =  '".$obj["results"][0]["geometry"]["location"]["lng"]."' WHERE  `place_locator`.`place_id` = ".$i, $this->db) or die('Invalide : ' . mysql_error());

                curl_close($ch);
            }
        }

它适用于 10 的循环,当达到 1000 时,将花费大量时间,并且许多结果没有更新到数据库中。

我认为可能是多线程应该有帮助,但我真的不知道它是如何工作的,请帮助我。

提前致谢

4

1 回答 1

2

I had the same problem. Google limits the frequency of the requests! Try a sleep(1); in the loop and it will work but need much more time.

于 2013-08-04T18:03:27.060 回答