12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- /**
- * 腾讯地图模块
- */
- namespace App\Servers;
- class TxMapServer
- {
- private $key = '6MABZ-FUWKJ-XB2FT-FOT46-HDXU5-HHBLO';
- static private $server = null;
- function __construct()
- {
- }
- /**
- * 创建对象
- * @return TxMapServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) self::$server = new TxMapServer();
- return self::$server;
- }
- /**
- * 获取定位信息
- * @param $ip
- * @return mixed
- */
- function getLocation($ip)
- {
- $url = 'https://apis.map.qq.com/ws/location/v1/ip';
- $data = $this->sendRequest($url, 'get', ['ip' => $ip]);
- return $data;
- }
- /**
- * 根据经纬度获取地理位置信息
- * @param $lat
- * @param $lng
- * @return mixed
- */
- function getLocationAddress($lat, $lng)
- {
- $url = 'https://apis.map.qq.com/ws/geocoder/v1/';
- $data = $this->sendRequest($url, 'get', ['location' => "{$lat},{$lng}"]);
- return $data;
- }
- /**
- * @param $url
- * @param string $type 请求方式
- * @param string $data 数据 数组格式
- * @return mixed
- */
- protected function sendRequest($url, $type = 'get', $data = [])
- {
- $data['key'] = $this->key;
- $ch = curl_init();
- if ($type == 'get' && $data) {
- $url = $url . '?' . http_build_query($data);
- }
- curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $head = array('User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36');
- curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
- if ($type == 'post') {
- curl_setopt($ch, CURLOPT_POST, TRUE);//post请求
- $data = json_encode($data, JSON_UNESCAPED_UNICODE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置传递的参数
- }
- $request = curl_exec($ch);
- curl_close($ch);
- $request = json_decode($request, true);
- return $request;
- }
- }
|