標籤:
今天寫一個PHP指令碼,目的是讓先從資料庫拿取響應的地區名 然後通過幼兒園的名字來查詢準確的經緯度。此間每次產生的經緯度進入資料庫內的更改。
7萬多條資料用時一個小時執行完畢。 不得不說 用curl結果還是比file_getcoents快的多。話不多說直接上代碼
<?phpdate_default_timezone_set(‘Asia/Chongqing‘);header(‘content-type:text/html; charset=utf-8‘);ini_set(‘display_errors‘, 1);error_reporting(E_ALL);set_time_limit(0);/** * @author gf * 擷取經緯度修改資料 * Class Importkindgarten */class Importkindgarten{ private $dbhost = ‘127.0.0.1‘; private $dbuser = ‘root‘; private $dbpw = ‘123‘; private $dbname = ‘mypolyguide‘; private $dbcharset = ‘utf8‘; private $write_db = null; public function __construct() { $this->db_write_link(); } /** * 串連資料庫 * @return null|resource */ public function db_write_link() { $this->write_db = mysql_connect($this->dbhost, $this->dbuser, $this->dbpw); if (!$this->write_db) { die(‘Could not connect: ‘ . mysql_error()); } mysql_query(‘SET character_set_connection=‘ . $this->dbcharset . ‘, character_set_results=‘ . $this->dbcharset . ‘, character_set_client=binary‘, $this->write_db); mysql_query(‘SET names utf8‘, $this->write_db); mysql_select_db($this->dbname, $this->write_db); return $this->write_db; } /** * 執行死迴圈進行查詢修改 */ public function roundKgList() { $i = 0; while(true) { $res = $this->getKgList(); if(empty($res[‘kgdata‘])) { break; } else { if(!empty($res[‘kgdata‘])) foreach($res[‘kgdata‘] as $k1=>$v1) { $info = $this->getAreanameById($v1[‘provinceId‘]); $area = $info[0]; $address=$v1[‘kgName‘]; $returnArr = $this->curlGetWeb($area,$address); $kgPosition = implode(‘,‘,$returnArr); $updatesql = "UPDATE kindgartenbak SET lng=‘{$returnArr[‘lng‘]}‘, lat=‘{$returnArr[‘lat‘]}‘, kgPosition=‘{$kgPosition}‘,runflag=‘1‘ WHERE kgId={$v1[‘kgId‘]}"; mysql_query($updatesql, $this->write_db); $i++; } else { break; } } } } /** * 擷取列表 * @return mixed */ public function getKgList() { $sql = ‘SELECT kgId,provinceId,kgName FROM kindgartenbak WHERE deleteFlag = 1 AND status = 2 AND runflag = 0 ORDER BY kgId DESC LIMIT 100‘; $query = mysql_query($sql, $this->write_db); while($result = mysql_fetch_array($query,MYSQL_ASSOC)) { $res[‘kgdata‘][] = $result; } return $res; } /** * 拿取城市 * @return mixed */ public function getAreanameById($provinceId) { $sql = ‘SELECT areaname FROM area WHERE id =‘.$provinceId; $query = mysql_query($sql, $this->write_db); $info = mysql_fetch_row($query); if(!empty($info)) { return $info; } else { return false; } } /** * 從百度地圖api以curl擷取經緯度 * 數組形式返回 */ public function curlGetWeb($area,$address) { $Url="http://api.map.baidu.com/geocoder?address=".trim($area).trim($address)."&output=json&key=96980ac7cf166499cbbcc946687fb414"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $Url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果把這行注釋掉的話,就會直接輸出 $result=curl_exec($ch); curl_close($ch); $infolist=json_decode($result); $array=array(); if(isset($infolist->result->location) && !empty($infolist->result->location)){ $array=array( ‘lng‘=>$infolist->result->location->lng, ‘lat‘=>$infolist->result->location->lat, ); return $array; } else { return false; } }}$start_date = date(‘Y-m-d H:i:s‘);$start = microtime(true);$db = new Importkindgarten();$i = $db->roundKgList();$end = microtime(true);echo ‘重設經緯度:‘.$start_date.‘----用時--‘.($end-$start).‘----共更新(‘.$i.‘)條‘;?>
一個PHP指令碼,通過curl先擷取百度地圖api產生的經緯度,然後改資料庫內的資料。