将大量地点转换为纬度&;使用PHP更新MySQL数据库
我是PHP新手,我想获得一个地方的纬度和经度,然后将它们添加到MySQL数据库中 我正在使用谷歌地理代码API来获取它们,这就是我现在所做的将大量地点转换为纬度&;使用PHP更新MySQL数据库,php,mysql,multithreading,Php,Mysql,Multithreading,我是PHP新手,我想获得一个地方的纬度和经度,然后将它们添加到MySQL数据库中 我正在使用谷歌地理代码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
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);
}
}
for($i=0;$idb)或die('invalide request:'.mysql_error());
if(mysql_num_行($sql)){
而($place=mysql\u fetch\u assoc($sql)){
//对我得到的位置字符串进行编码,以消除空格
$encodePlace=str_replace(“,“%20”,“$place[“place_address”);
//使用谷歌API
$url='1http://maps.googleapis.com/maps/api/geocode/json?address=“.$encodePlace.”&sensor=false';
//使用Curl发送请求
$ch=curl\u init($url);
curl_setopt($ch,CURLOPT_头,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$response=curl\u exec($ch);
$obj=json_decode($response,true);
$updateSql=mysql\u查询(“更新`place\u locator`.`place\u locator`SET
`纬度“=”$obj[“结果”][0][“几何体”][“位置”][“纬度”]。“,
`经度“=”$obj[“结果”][0][“几何体”][“位置”][“液化天然气”]。“'place\u locator`.'place\u id`=”$i、$this->db)或死亡('Invalide:'.mysql\u error());
卷曲关闭($ch);
}
}
它适用于10个循环,当达到1000个循环时,需要花费大量时间,并且许多结果没有更新到数据库中
我认为多线程可能会有所帮助,但我真的不知道它是如何工作的,请帮助我
提前谢谢我也有同样的问题。谷歌限制了请求的频率!尝试
睡眠(1)代码>在循环中运行,但需要更多的时间。给出的答案可能正是您需要的:它讨论了如何使用curl\u multi\u xxx“同时”发出和处理多个请求。
谷歌可能会限制您发出这些请求的频率。如果某些请求返回时数据为空,您可能实际上希望减慢请求的速度,可以在其中插入sleep()
。这无疑会降低运行速度,但如果这是问题所在,则必须为每个请求获取适当的数据。