### 用户信息
* * * * *
用户信息的获取并不难,在封装的SDK中,有写好的方法
~~~
public function get_user_info($openid)
{
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
$res = $this->http_request($url);
return json_decode($res, true);
}
~~~
而$access_token,在__construct中已经得到了:
~~~
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid && $appsecret){
$this->appid = $appid;
$this->appsecret = $appsecret;
}
//本地写入
$res = file_get_contents('access_token.json');
$result = json_decode($res, true);
$this->expires_time = $result["expires_time"];
$this->access_token = $result["access_token"];
if (time() > ($this->expires_time + 600)){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
$res = $this->http_request($url);
$result = json_decode($res, true);
$this->access_token = $result["access_token"];
$this->expires_time = time();
file_put_contents('access_token.json', '{"access_token": "'.$this->access_token.'", "expires_time": '.$this->expires_time.'}');
}
}
~~~
在此提一下,需要public目录建立access_token.json文件。
不难看出,只要拿到用户的openid即可获取用户的信息。
以用户发送的文本消息为例:
~~~
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>
~~~
我们取得 `FromUserName` 就是需要的openid。
~~~
$openid = strval($object->FromUserName);
~~~
所以,调用get_user_info($openid)方法就能得到用户信息。
- 序言
- 联系方式,可一对一咨询
- 准备
- 安装twothink
- 第一章 公众号推广海报(已完成17.10.10)
- 0 整体思路
- 1.1 公众号接口绑定
- 1.2 引入微信SDK
- 1.3 响应文本请求
- 1.4 响应菜单请求
- 1.5 获取用户的信息
- 1.6 生成参数二维码
- 1.7 扫描参数二维码
- 1.8 建立数据表
- 1.9 处理关注事件
- 1.10 完整海报
- 1.11 推送海报
- 第二章 企业网站搭建
- 第三章 Excel表格处理(已完成17.12.04)
- 1.1 导入phpExcel
- 1.2 读取表单上传的excel表格
- 1.3 导出内容到表格
- 第四章 简易商城
- 1.1 梳理逻辑
- 1.2 商品上传
- 第五章 基于公号的任务系统
- 0 把逻辑理清楚
- 1.1 后台新建分类
- 1.2 完善tdetail方法
- 1.3 完善share方法
- 1.4 任务列表
- 第六章 前端使用mdui
- 1 基础页面
- 第七章 前端使用element(未来更新)
- 第八章 使用QueryList采集目标页面