Skip to content

Commit e2ca726

Browse files
committed
code init
0 parents  commit e2ca726

File tree

9 files changed

+547
-0
lines changed

9 files changed

+547
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor
2+
index.php
3+
.idea
4+
composer.lock

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 腾讯地图webservices的php封装
2+
## 安装
3+
4+
---
5+
```shell
6+
composer require death_satan/tencent-map-api -vvv
7+
```
8+
---
9+
10+
## 使用
11+
12+
---
13+
```php
14+
$key = '';//腾讯地图key
15+
$app = new \DeathSatan\TencentMapApi\Application($key);
16+
17+
//地址转经纬度
18+
$data=$app->api()->addressResolution('北京市');
19+
var_dump($data);
20+
/**
21+
* echo
22+
*
23+
["adcode"]=>
24+
string(6) "110114"
25+
}
26+
["address_components"]=>
27+
array(5) {
28+
["province"]=>
29+
string(9) "北京市"
30+
["city"]=>
31+
string(9) "北京市"
32+
["district"]=>
33+
string(9) "昌平区"
34+
["street"]=>
35+
string(0) ""
36+
["street_number"]=>
37+
string(0) ""
38+
}
39+
["similarity"]=>
40+
float(0.8)
41+
["deviation"]=>
42+
int(1000)
43+
["reliability"]=>
44+
int(1)
45+
["level"]=>
46+
int(2)
47+
}
48+
*/
49+
```
50+
---

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "death_satan/tencent-map-api",
3+
"description": "tencent map services api,based on GuzzleHttp",
4+
"type": "library",
5+
"require": {
6+
"guzzlehttp/guzzle": "7.4.x-dev",
7+
"ext-json": "*"
8+
},
9+
"license": "MIT",
10+
"autoload": {
11+
"psr-4": {
12+
"DeathSatan\\TencentMapApi\\": "src/"
13+
}
14+
},
15+
"authors": [
16+
{
17+
"name": "ShuSRun",
18+
"email": "[email protected]"
19+
}
20+
],
21+
"minimum-stability": "dev"
22+
}

src/Application.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace DeathSatan\TencentMapApi;
4+
5+
use DeathSatan\TencentMapApi\Exception\FailException;
6+
use GuzzleHttp\Client;
7+
8+
/**
9+
* 管理中心
10+
*/
11+
class Application
12+
{
13+
/**
14+
* 当前版本
15+
*/
16+
public const VERSION = '1.0';
17+
18+
/**
19+
* guzzleHttp客户端
20+
* @var Client
21+
*/
22+
protected $client;
23+
24+
protected $key;
25+
26+
/**
27+
* 默认配置
28+
* @var array
29+
*/
30+
protected $default_config = [
31+
//基础域名
32+
'base_uri'=>'https://apis.map.qq.com/ws/',
33+
//是否进行ssl验证
34+
'verify'=>false,
35+
//请求超时的秒数
36+
'timeout'=>5.0,
37+
];
38+
39+
/**
40+
* @param string|null $key api请求所需要的key
41+
* @param array $config guzzleHttp Client客户端配置,传空使用默认配置
42+
* @throws FailException
43+
*/
44+
public function __construct(?string $key=null,array $config=[])
45+
{
46+
if (empty($key)){
47+
throw new FailException('Configuration item key required');
48+
}
49+
$this->key = $key;
50+
$config = array_merge($this->default_config,$config);
51+
$this->client = new Client($config);
52+
}
53+
54+
/**
55+
* @return Client
56+
*/
57+
public function getClient(): Client
58+
{
59+
return $this->client;
60+
}
61+
62+
/**
63+
* 获取api请求类
64+
* @return Server
65+
*/
66+
public function api()
67+
{
68+
return new Server(
69+
(new Request($this->key,$this->client))
70+
);
71+
}
72+
}

src/Exception/FailException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace DeathSatan\TencentMapApi\Exception;
4+
5+
/**
6+
* 基础错误信息异常
7+
*/
8+
class FailException extends \Exception
9+
{
10+
11+
}

src/Exception/ServerException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace DeathSatan\TencentMapApi\Exception;
4+
5+
/**
6+
* 服务端异常
7+
*/
8+
class ServerException extends \Exception
9+
{
10+
11+
}

src/Request.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace DeathSatan\TencentMapApi;
4+
5+
use DeathSatan\TencentMapApi\Exception\ServerException;
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\Exception\GuzzleException;
8+
9+
/**
10+
* api操作类
11+
*/
12+
final class Request
13+
{
14+
/**
15+
* @var string
16+
*/
17+
protected $key;
18+
19+
/**
20+
* @var Client
21+
*/
22+
protected $client;
23+
24+
protected $response;
25+
26+
public function __construct($key,Client $client)
27+
{
28+
$this->key = $key;
29+
$this->client = $client;
30+
}
31+
32+
/**
33+
* 发送get请求
34+
* @throws GuzzleException
35+
*/
36+
public function get($url, $data=[]): Response
37+
{
38+
return $this->beforeResponse(
39+
$this->client->request('get',$url,[
40+
'query'=>array_merge($data,[
41+
'key'=>$this->key
42+
])
43+
])
44+
);
45+
}
46+
47+
/**
48+
* 请求后前置处理response
49+
* @param \GuzzleHttp\Psr7\Response $response
50+
* @return Response
51+
*/
52+
protected function beforeResponse(\GuzzleHttp\Psr7\Response $response): Response
53+
{
54+
$this->response = new Response($response);
55+
if ($this->response['status']!==0){
56+
throw new ServerException($this->response['message']);
57+
}
58+
return $this->response();
59+
}
60+
61+
/**
62+
* 获取相应
63+
* @return Response
64+
*/
65+
private function response(): Response
66+
{
67+
return $this->response;
68+
}
69+
}

src/Response.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace DeathSatan\TencentMapApi;
4+
5+
use JetBrains\PhpStorm\Internal\LanguageLevelTypeAware;
6+
use JetBrains\PhpStorm\Internal\TentativeType;
7+
8+
/**
9+
* 响应类
10+
*/
11+
class Response implements \ArrayAccess
12+
{
13+
/**
14+
* @var \GuzzleHttp\Psr7\Response
15+
*/
16+
protected $response;
17+
18+
/**
19+
* @var array
20+
*/
21+
protected $data;
22+
23+
public function __construct(\GuzzleHttp\Psr7\Response $response)
24+
{
25+
$this->response = $response;
26+
$this->data = json_decode($this->rawData(),JSON_UNESCAPED_UNICODE);
27+
}
28+
29+
protected function rawData()
30+
{
31+
return (string)$this->response->getBody();
32+
}
33+
34+
/**
35+
* 获取数组
36+
* @return array
37+
*/
38+
public function toArray():array
39+
{
40+
return $this->data['result'];
41+
}
42+
43+
public function offsetExists($offset)
44+
{
45+
return empty($this->data[$offset]);
46+
}
47+
48+
public function offsetGet($offset)
49+
{
50+
return $this->data[$offset];
51+
}
52+
53+
public function offsetSet($offset, $value)
54+
{
55+
$this->data[$offset]=$value;
56+
}
57+
58+
public function offsetUnset($offset)
59+
{
60+
unset($this->data[$offset]);
61+
}
62+
}

0 commit comments

Comments
 (0)