一個PHP指令碼,通過curl先擷取百度地圖api產生的經緯度,然後改資料庫內的資料。

來源:互聯網
上載者:User

標籤:

今天寫一個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產生的經緯度,然後改資料庫內的資料。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.