微信开发之助手类

供微信开发的助手类

其中调用的部分函数为 ThinkPHP 3.2 专有,可用其他函数替换。
请先配置好 $this->appid 和 $this->secret。

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
<?php
/**
* 微信开发常用方法
* getAccessToken------获取 access_token
* getJsApiTicket------获取 Jssdk ticket
* getSignPackage------获取 Jssdk 签名包
*
* sendTplMsg----------发送模板消息
* sendText------------发送文字消息
* getQRCode-----------生成带参二维码
*/
/**
* 基类
*/
class Common {
public function __construct() {
$this->access_token = $this->getAccessToken();
// $this->appid = "";
// $this->secret = "";
}
public function curlGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
/* 发送post请求 */
public function curlPost($url = '', $param = ''){
if (empty($url) || empty($param)) return false;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
public function getAccessToken() {
$access_token = S($this->appid);
if($access_token == false){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
$res = json_decode($this->curlGet($url));
$access_token = $res->access_token;
S($this->appid,$access_token,7000);
}
return $access_token;
}
public function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
}
class Jssdk extends Common {
public function __construct(){
parent::__construct();
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
// 注意 URL 一定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appid" => $this->appid,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
public function getJsApiTicket() {
$ticket = S('jsapi_ticket');
if($ticket == false){
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->access_token;
$res = json_decode($this->curlGet($url));
$ticket = $res->ticket;
S('jsapi_ticket',$ticket,7000);
}
return $ticket;
}
}
class TplMsg extends Common {
public function __construct(){
parent::__construct();
}
/**
* [sendTplMsg 发送模版消息]
* @param [string] $touser [OpenID]
* @param [string] $template_id [模板ID]
* @param [string] $url [详情链接]
*/
public function sendTplMsg($touser, $template_id, $url, $data){
$parm = array(
'touser' => $touser,
'template_id' => $template_id,
'url' => $url,
'data' => $data
);
$data = json_encode($parm);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->access_token;
$result = $this->curlPost($url, urldecode($data));
$res = json_decode($result,true);
return $res['errcode']==0?true:false;
}
/**
* [sendText 发送文字消息]
* @param [string] $openid [OpenID]
* @param [string] $text [内容]
*/
public function sendText($openid,$text){
$parm = array(
'touser' => $openid,
'msgtype' => "text",
'text' => array("content"=>$text)
);
// 可定义客服帐号
// $parm['customservice'] = array("kf_account"=>"kf2001@gh_88f7b3500cce");
$data = json_encode($parm,JSON_UNESCAPED_UNICODE);
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
$result = $this->curlPost($url, urldecode($data));
$res = json_decode($result,true);
return $res;
return $res['errcode']==0?true:false;
}
/**
* [getKfAccount 通过微信号获得 kf_account]
*/
public function getKfAccount(){
$kf_wx = "ChasenTsai";
$url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=".$this->access_token;
$result = $this->curlGet($url);
$res = json_decode($result,true);
foreach($res['kf_list'] as $val){
if($val['kf_wx'] == $kf_wx){
$kf_account = $val['kf_account'];
}
}
echo $kf_account?:false;
}
/**
* [getQRCode 生成带参二维码]
* @param [int] $scene_id
*/
public function getQRCode($scene_id){
$qrcode = '{"action_name":"QR_LIMIT_SCENE","action_info":{"scene":{"scene_id": '.$scene_id.'}}}';
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->access_token;
$result = $this->curlPost($url,$qrcode);
$res = json_decode($result,true);
return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$res['ticket'];
}
}