今天讲讲利用微信oauth2实现第三方登录的实现方法.php
先说说前提吧!html
首先你得是服务号,而且是通过认证的.这样微信会给你不少第三方接口的权限,若是是订阅号或者没有认证的服务号那就不用想了!前端
一开始你须要进入微信公众平台开启开发模式,而且填写oauth2的回调地址,地址填写你项目的域名就能够了.好比:www.baidu.com或zhidao.baidu.com.若是你的项目在二级域名就写二级域名数据库
前端url受权地址,在url中填写appid与你项目中方法中的oauth的地址,具体在下面的代码中能够看到.json
<ahref="https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=http://www.xxxxxx.com/action/function/oauth2&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect">受权</a>
再说后台逻辑,首先调用微信接口的SDK.(后面会有)api
include('./Card/Common/class_weixin_adv.php');
以后填入微信官方给的的appid与secretruby
$weixin=new class_weixin_adv("appid", "secret");
初始化SDK的类,取到code,利用获取到的code在获取出openid 看下面代码注释!微信
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$_GET['code']."&grant_type=authorization_code"; $res = $weixin->https_request($url);//调用SDK方法获取到res 从中能够获得openid $res=(json_decode($res, true));//转换成array 方便调用openid
继续调用SDK方法,获取到用户信息.此时$row已经得到用户信息了 能够var_dump下看看键值方便存入数据库cookie
$row=$weixin->get_user_info($res['openid']);
获取用户信息就大功告成了,但这还不够.咱们须要的是无需注册!因此须要利用openid,openid属于惟一凭证,每一个用户对不一样的公众号都有不一样的openid.能够理解成用户帐号的感受.我这里用的是把openid存入cookie的解决方案,相似用户登录的感受,一些关键数据验证只须要与数据库中的openid进行对比.其余的一些利用方法能够发挥你们的想象!能够跟我留言交流!app
关于以前的a连接的受权,你们也能够判断cookie是否存在openid,从而让未受权用户直接跳转到该地址,省却了用户的一步操做.
下面是完整逻辑代码,你们能够参考下!
public function oauth2(){ include('./Card/Common/class_weixin_adv.php'); $weixin=new class_weixin_adv("appid", "secret"); if (isset($_GET['code'])){ $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$_GET['code']."&grant_type=authorization_code"; $res = $weixin->https_request($url); $res=(json_decode($res, true)); $row=$weixin->get_user_info($res['openid']); if ($row['openid']) { //这里写上逻辑,存入cookie,数据库等操做 cookie('weixin',$row['openid'],25920000); }else{ $this->error('受权出错,请从新受权!'); } }else{ echo "NO CODE"; } $this->display(); }
SDK代码:微信官方有手册,我就很少讲了,本身研究,很简单的!.
<?php
/** * 微信SDK * pan041ymail@gmail.com */ class class_weixin_adv { var $appid = ""; var $appsecret = ""; //构造函数,获取Access Token public function __construct($appid = NULL, $appsecret = NULL) { if($appid){ $this->appid = $appid; } if($appsecret){ $this->appsecret = $appsecret; } $this->lasttime = 1395049256; $this->access_token = "nRZvVpDU7LxcSi7GnG2LrUcmKbAECzRf0NyDBwKlng4nMPf88d34pkzdNcvhqm4clidLGAS18cN1RTSK60p49zIZY4aO13sF-eqsCs0xjlbad-lKVskk8T7gALQ5dIrgXbQQ_TAesSasjJ210vIqTQ"; if (time() > ($this->lasttime + 7200)){ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret; $res = $this->https_request($url); $result = json_decode($res, true); $this->access_token = $result["access_token"]; $this->lasttime = time(); } } //获取用户基本信息 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->https_request($url); return json_decode($res, true); } //https请求 public function https_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } }
转载请注明出处:http://home.cnblogs.com/u/p-0day/