微信公众号基础02_获取accessToken和用户信息

时间:2023-03-09 09:16:43
微信公众号基础02_获取accessToken和用户信息

上一篇分享了搭建微信公众号server,本文分享一下假设获取access_Token和用户信息。工具还是新浪云SAE

1.获取access_Token

相见开发文档:https://mp.weixin.qq.com/wiki/14/9f9c82c1af308e3b14ba9b973f99a8ba.html

accesstoken是公众号的全局唯一票据,公众号调用各接口时都需使用accesstoken。开发人员须要进行妥善保存。accesstoken的存储至少要保留512个字符空间。accesstoken的有效期眼下为2个小时,需定时刷新。反复获取将导致上次获取的access_token失效。

请求地址 2小时期限 每天获取次数有限 须要保存下来,过期再又一次获取
https://api.weixin.qq.com/cgi-bin/token?granttype=clientcredential&appid=APPID&secret=APPSECRET

获取access_token一般包括两个文件,access_token.php文件用于推断access_token.txt文件里的acess_token是否到期并又一次获取放在access_token.txt文件里。

access_token.txt文件

{"access_token":"","time":0}

accesstoken:保存获取到的accesstoken值。time:记录获取时间

access_token.php文件

<?

php
$appid = '此处填写appid';
$secret = '此处填写secret';
//请求腾讯接口 获取token 測试号每天2000次
// 获取access_token地址
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
// 获取access_token文件信息
$fileCon = file_get_contents("access_token.txt");
$fileJson = json_decode($fileCon); // 推断access_token是否过期,在接近两小时的7000秒时。又一次获取一次,保存在access_token.txt中
if ($fileJson->time<time()-7000) {
// 通过接口又一次获取access_token
$str = file_get_contents($url);
$json = json_decode($str);//把json字符串转为json对象 $access_token = $json->access_token; $data = array("access_token"=>$access_token,"time"=>time());
$json_str = json_encode($data); // 保存获取到的access_token
file_put_contents("access_token.txt", $json_str);
}else{
$access_token = $fileJson->access_token;
} echo $access_token;//输出查看access_token
?>

2.git上传至新浪云測试

打开新浪云SAE。选择自己的应用。在左側选择数据库与缓存服务,点开memcached,创建新的memcached用于存放缓存的access_token

微信公众号基础02_获取accessToken和用户信息

注意上传至新浪云之前须要在access_token.php文件里的access_token.txt前面加上saemc://这是新浪云SAE訪问缓存文件的前缀,假设在本地server測试。则不须要加。

上传上去后,在浏览器输入应用地址后面跟上/access_token.php就可以返回一个access_token值。

3.获取用户信息

在測试号管理页面http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index通过微信扫码关注測试号,在用户列表里有了用户openid。有了access_token和openid才干获取关注者的信息。

參考获取用户信息文档http://mp.weixin.qq.com/wiki/1/8a5ce6257f1d3b2afb20f83e72b72ce9.html

使用OpenID来获取用户基本信息。

仅仅需在access_token.php文件里加入相关代码就可以

<?

php
$appid = '此处填写appid';
$secret = '此处填写secret';
//请求腾讯接口 获取token 測试号每天2000次
// 获取access_token地址
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
// 获取access_token文件信息
$fileCon = file_get_contents("saemc://access_token.txt");
$fileJson = json_decode($fileCon); // 推断access_token是否过期
if ($fileJson->time<time()-7000) {
// 通过接口又一次获取access_token
$str = file_get_contents($url);
$json = json_decode($str);//把json字符串转为json对象 $access_token = $json->access_token; $data = array("access_token"=>$access_token,"time"=>time());
$json_str = json_encode($data); // 保存获取到的access_token
file_put_contents("saemc://access_token.txt", $json_str);
}else{
$access_token = $fileJson->access_token;
} // echo $access_token; // 用户openID
$openid = '当中一位关注者的openId';
// 获取用户信息地址
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
//获取接口信息
$user = file_get_contents($url);
// echo $user;
// 把获取的信息转为json对象
$obj = json_decode($user);
// 输出表格显示获取到的信息
echo "<table>";
echo "<tr>
<td><img style='width:60px' src='{$obj->headimgurl}'</td>
<td>{$obj->nickname}</td>
<td>".($obj->sex==1? "男":"女")."</td>
<td>{$obj->city}</td>
</tr>";
echo "</table>"; ?>

将以上代码更新至新浪云。打开access_token.php会显示某位用户相关信息

4.获取用户信息列表

当关注者不止一位时,能够输出用户信息列表。

在access_token.php中使用循环输出用户信息列表

<?php
$appid = '此处填写appid';
$secret = '此处填写secret';
//请求腾讯接口 获取token 測试号每天2000次
// 获取access_token地址
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
// 获取access_token文件信息
$fileCon = file_get_contents("saemc://access_token.txt");
$fileJson = json_decode($fileCon); // 推断access_token是否过期
if ($fileJson->time<time()-7000) {
// 通过接口又一次获取access_token
$str = file_get_contents($url);
$json = json_decode($str);//把json字符串转为json对象 $access_token = $json->access_token; $data = array("access_token"=>$access_token,"time"=>time());
$json_str = json_encode($data); // 保存获取到的access_token
file_put_contents("saemc://access_token.txt", $json_str);
}else{
$access_token = $fileJson->access_token;
} // echo $access_token; //获取用户列表 $url = "https://api.weixin.qq.com/cgi-bin/user/get? access_token={$access_token}";
$list = file_get_contents($url);
$listObj = json_decode($list);
// var_dump($listObj);
// exit(); //循环输出用户列表
$arr = $listObj->data->openid;
for($i = 0; $i <count($arr);$i ++){
// 用户openID
$openid = $arr[$i];
// 获取用户信息地址
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
//获取接口信息
$user = file_get_contents($url);
// echo $user;
// 把获取的信息转为json对象
$obj = json_decode($user);
// 输出表格显示获取到的信息
echo "<table>";
echo "<tr>
<td><img style='width:60px' src='{$obj->headimgurl}'</td>
<td>{$obj->nickname}</td>
<td>".($obj->sex==1?"男":"女")."</td>
<td>{$obj->city}</td>
</tr>";
echo "</table>";
}
?>