WeChat Official Accounts with Access_token and Menu

WeChat Official Accounts: Access_token and Menu

Exchange blogroll: laker.me
Github:https://github.com/younglaker

Get Access_token

Add your Official Accounts appID and appsecret to get access_token, and remember it.

https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%E5%9F%BA%E7%A1%80%E6%94%AF%E6%8C%81&form=%E8%8E%B7%E5%8F%96access_token%E6%8E%A5%E5%8F%A3%20/token

In your database, new a table using this sql code, and modify your Official Accounts appID, appsecret and access_token in 'your_appid', 'your_appsecret', 'your_access_token'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
CREATE TABLE IF NOT EXISTS `wxtoken` (
`id` int(11) NOT NULL,
`appid` varchar(100) NOT NULL,
`appsecret` varchar(100) NOT NULL,
`access_token` varchar(1024) DEFAULT NULL,
`AccessExpires` int(11) DEFAULT NULL,
`TIMESTAMP` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO `wxtoken` (`id`, `appid`, `appsecret`, `access_token`, `AccessExpires`, `TIMESTAMP`) VALUES
(1, 'your_appid', 'your_appsecret', 'your_access_token', 1543244037, '2018-11-26 12:53:57');
ALTER TABLE `wxtoken`
ADD PRIMARY KEY (`id`);
ALTER TABLE `wxtoken`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;

And a wx directory in ThinkPHP project:

wx directory

New a controller directory and index.php:

controller

Add this code to Index.php, and modify your Official Accounts appID and appsecret in "你的公众号appid","你的公众号appsecret" :

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
public function index()
{
$access_token = $this->getToken();
echo $access_token;
}
function getToken(){
return $this->checkAccessToken("你的公众号appid","你的公众号appsecret");
}
function checkAccessToken($appid,$appsecret){
$condition = array('appid'=>$appid,'appsecret'=>$appsecret);
$access_token_set=DB('wxtoken')->where($condition)->find();//获取数据
if($access_token_set){
//检查是否超时,超时了重新获取
if($access_token_set['AccessExpires']>time()){
//未超时,直接返回access_token
return $access_token_set['access_token'];
}else{
//已超时,重新获取
$url_get='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
$json= $this->https_request($url_get);
var_dump($json);
$access_token=$json['access_token'];
$AccessExpires=time()+intval($json['expires_in']);
$data['access_token']=$access_token;
$data['AccessExpires']=$AccessExpires;
$result = DB('wxtoken')->where($condition)->update($data);//更新数据
if($result){
return $access_token;
}else{
return $access_token;
}
}
}else{
echo "appid或appsecret不正确";
return false;
}
}
function https_request ($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$out = curl_exec($ch);
curl_close($ch);
return json_decode($out,true);
}

Code

Visit your api:

Visit

Create a menu in Official Accounts

Modify index():

1
2
3
4
5
public function index()
{
$menu = $this->createmenu();
echo $menu;
}

Add this code into index.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
public function createmenu(){
$data='{
"button":[
{
"type":"click",
"name":"今日歌曲1",
"key":"V1001_TODAY_MUSIC"
},
{
"name":"菜单",
"sub_button":[
{
"type":"view",
"name":"搜索",
"url":"http://www.soso.com/"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_GOOD"
}]
}]
}';
$access_token=$this->getToken();
$url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token;
var_dump($url);
var_dump($data);
$result= $this->postcurl($url,$data);
var_dump($result);
}
function postcurl($url,$data = null){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output=json_decode($output,true);
}

Visit the api /index.php/wx/index/index:

Visit

Visit the Official Account:

Visit

Get user data

Modify index():

1
2
3
4
5
public function index()
{
$data = $this->getuser();
echo $data;
}

Add this code into index.php:

1
2
3
4
5
6
7
8
9
public function getuser(){
$access_token=$this->getToken();
$url_get='https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$access_token;
$user_json= $this->https_request($url_get);
//var_dump($json);
$url_get='https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$user_json['data']['openid'][0].'&lang=zh_CN';
$user_info= $this->https_request($url_get);
var_dump($user_info);
}

Visit the api /index.php/wx/index/index:

Visit

在此输入正文


WeChat Official Accounts with Access_token and Menu

本文原创自http://laker.me/blog,转载请注明出处,欢迎交换友链

如果本文对您有帮助,微信扫一扫,请我吃个鸡腿吧

评论组件不稳定,有事请联系V信 lakerHQ (请备注来自博客)