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

上一篇分享了搭建微信公众号服务器,本文分享一下如果获取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



注意上传至新浪云之前需要在access_token.php文件中的access_token.txt前面加上saemc://这是新浪云SAE访问缓存文件的前缀,如果在本地服务器测试,则不需要加。

 

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

 

3.获取用户信息

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

参考获取用户信息文档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>";
}
 ?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值