### 用户信息 * * * * * 用户信息的获取并不难,在封装的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)方法就能得到用户信息。