今天这篇教程主要教大家如何获取微信用户信息,这个只要在做微信开发就会被用到,有很多朋友犯难,那么今天写一篇教程,大家根据教程一步一步的走下去绝壁是没有问题的!嘿嘿嘿,下面我们start!
开发钱准备(已经通过认证过的微信公众帐号、服务器一台、thinkphp5.0项目)
一、微信公众号设置
登录微信公众帐号点击接口权限查看网页授权是否获得
点击修改设置网页授权域名
设置如下点击确定即可
回到之前的页面把业务域名和js安全域名也一道设置一下吧,虽然没啥影响哈!
额,设置好了,我们还需要一个东西,一个是微信公众号的appid 一个是secret,点击开发下面的基本配置就可以看到了!嘿嘿嘿,打了马赛克!我们在下面会用到!
二、编写PHP代码
来个前奏嘛?(额,微信上的文档这里就补贴了哈,原理啥的我们写完了在总结下)!嘿嘿嘿!一言不合就上代码,
上代码之前我说说哈,我在application下新建了个service模块专门来处理第三方类,当然大伙也可以放在其他的地方根据个人喜好来!
在service新建一个Wechat.php类,大家期待已久的代码终于要贴上来了,嘿嘿嘿!这些代码都是根据微信接口文档来封装的,大家直接用就得了!
<?php namespace app\service; use think\Controller; class Wechat extends Controller{ protected $appid='你的微信公众号appid'; protected $appsecret = '你的微信公众号secret '; public function accredit($redirect_url){ $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect_url}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; $this->redirect($url); } /** * @param $code * @return bool|string */ public function getAccessToken($code){ $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code"; $res = file_get_contents($url); //获取文件内容或获取网络请求的内容 $access_token = json_decode($res,true); return $access_token; } /** * 获取用户信息 * @param unknown $openid * @param unknown $access_token * @return unknown */ public function getWeChatUserInfo($access_token,$openid){ $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN"; $output = file_get_contents($url); $weChatUserInfo = json_decode($output,true); return $weChatUserInfo; } }
方法封装好了,我们准备来用一用了哈!我在application下建了个wechat模块,专门用来放微信开发方面的代码!
4.然后在wechat里面新建一个控制器Index.php,在控制器添加如下方法
<?php /** * Created by PhpStorm. * User: Tangyijun * Date: 2017/4/1 * Time: 10:58 */ namespace app\wechat\controller; use app\service\Wechat;//引入我们刚才新建在service下的wechat类 class Index extends Base{ /** * 微信按钮跳转授权 */ public function weChatAccredit(){ $url = 'http://'.$_SERVER['HTTP_HOST'].'/wechat/index/getChatInfo'; $we_chat = new Wechat(); //实例化类 $we_chat->accredit($url); //调用方法 } /** * 获取微信用户信息 */ public function getChatInfo(){ $we_chat = new Wechat();//实例化微信类 $code = $_GET['code']; //获取跳转后的code $access_token = $we_chat->getAccessToken($code); //根据code获取token //根据access_token和openid获取到用户信息 $we_chat_user_info = $we_chat->getWeChatUserInfo($access_token['access_token'],$access_token['openid']); var_dump($we_chat_user_info ); }
到此我们在微信浏览器中访问http://www.yourdomain.com/wechat/index/weChatAccredit 就能够获取到你的微信用户信息了。
总结:
嘿嘿嘿,是不是很简单,我们达到了效果可不能知其然不知其所以然啊,因为我们是一个优秀的程序猿不是!为毛要这样写,下面来说说!
我们主要利用的网页跳转授权的方式,因此我们在类Wechat.php中定义了一个方法accredit(),我们分析一下下面的代码
https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect_url}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect
大家有没有注意到有这样一个参数
redirect_uri
这个参数就是当我们访问这个方法授权之后将要跳转的页面,他在跳转之后会将这个code带到我们的页面,我们在这个页面就能获取到他的code,也就是为什么我要在index控制器中定义一个
getChatInfo()方法
在这个方法中有这样一段:
$code = $_GET['code']; //获取跳转后的code $access_token = $we_chat->getAccessToken($code);
对把我们就是用code去获得用户的token和openid,然后在得到用户的信息,具体的还需要大家看下文档才是最好的!
请问我的$code = $_GET['code']; 为什么获取不到用户信息
这个只是获取到code码,你需要根据code码调用相关的接口获取用户信息!
数组的access_token不存在,可以降低错误级别;需要具体看代码