forked from thenbsp/wechat
/
Wechat.php
197 lines (161 loc) · 4.83 KB
/
Wechat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Thenbsp\Wechat;
use Thenbsp\Wechat\Util\Http;
use Thenbsp\Wechat\Util\Cache;
use Thenbsp\Wechat\Exception\WechatException;
use Thenbsp\Wechat\Exception\TicketException;
use Thenbsp\Wechat\Exception\AccessTokenException;
/**
* 微信 SDK 核心类,提供访问公众号自身数据的能力,比如 AccessToken、Ticket 等
* Created by thenbsp (thenbsp@gmail.com)
* Created at 2015/07/23
*/
class Wechat
{
/**
* 公众号 AccessToken 接口地址
*/
const ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token';
/**
* 公众号 ticket 接口地址
*/
const TICKET_URL = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';
/**
* 获取公众号 IP 地址列表
*/
const SERVERIP_URL = 'https://api.weixin.qq.com/cgi-bin/getcallbackip';
/**
* Cache 文件缓存
*/
protected $cache;
/**
* 公众号 AppId
*/
protected $appid;
/**
* 公众号 AppSecret
*/
protected $appsecret;
/**
* 构造方法
*/
public function __construct($appid, $appsecret, $cacheDir = null)
{
$this->appid = $appid;
$this->appsecret = $appsecret;
try {
$this->cache = new Cache($cacheDir ?: __DIR__.DIRECTORY_SEPARATOR.'Storage');
} catch (\Exception $e) {
exit($e->getMessage());
}
}
/**
* 获取 Appid
*/
public function getAppid()
{
return $this->appid;
}
/**
* 获取 Appsecret
*/
public function getAppsecret()
{
return $this->appsecret;
}
/**
* 获取 AccessToken
*/
public function getAccessToken()
{
$key = $this->appid.'_access_token';
$cache = $this->cache->get($key);
if( $cache && !(empty($cache->access_token) ||
empty($cache->expires_in)) ) {
if( $cache->expires_in > time() ) {
// echo 'from cache';
return $cache->access_token;
}
}
// echo 'form api';
$response = $this->getAccessTokenResponse();
$response->expires_in += time();
$this->cache->set($key, $response);
return $response->access_token;
}
/**
* 获取 Ticket
*/
public function getTicket($type = 'jsapi')
{
$key = $this->appid.'_ticket_'.$type;
$cache = $this->cache->get($key);
if( $cache && !(empty($cache->ticket) ||
empty($cache->expires_in)) ) {
if( $cache->expires_in > time() ) {
// echo 'from cache';
return $cache->ticket;
}
}
// echo 'form api';
$response = $this->getTicketResponse($type);
$response->expires_in += time();
$this->cache->set($key, $response);
return $response->ticket;
}
/**
* 获取 AccessToken(从接口获取)
*/
public function getAccessTokenResponse()
{
$params = array(
'grant_type' => 'client_credential',
'appid' => $this->appid,
'secret' => $this->appsecret
);
$response = Http::get(static::ACCESS_TOKEN_URL, $params);
if( isset($response->access_token) &&
isset($response->expires_in) ) {
return $response;
}
throw new AccessTokenException($response->errcode.': '.$response->errmsg);
}
/**
* 获取 Ticket(从接口获取)
*/
public function getTicketResponse($type)
{
try {
$accessToken = $this->getAccessToken();
} catch (AccessTokenException $e) {
throw new TicketException($e->getMessage());
}
$params = array(
'access_token' => $accessToken,
'type' => $type,
);
$response = Http::get(static::TICKET_URL, $params);
if( isset($response->ticket) &&
isset($response->expires_in) ) {
return $response;
}
throw new TicketException($response->errcode.': '.$response->errmsg);
}
/**
* 获取微信服务器 IP
*/
public function getServerIp()
{
try {
$accessToken = $this->getAccessToken();
} catch (AccessTokenException $e) {
throw new WechatException($e->getMessage());
}
$params = array('access_token' => $accessToken);
$response = Http::get(static::SERVERIP_URL, $params);
if( isset($response->ip_list) ) {
return $response->ip_list;
}
throw new WechatException($response->errcode.': '.$response->errmsg);
}
}