当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP版微信第三方一键登录及获取用户信息开发教程

发布:smiling 来源: PHP粉丝网  添加日期:2018-10-30 09:42:12 浏览: 评论:0 

注意,要使用微信在第三方网页登录是需要“服务号”才可以哦,所以必须到官方申请。

一开始你需要进入微信公众平台开启开发模式,并且填写oauth2的回调地址,地址填写你项目的域名就可以了.比如:www.baidu.com或zhidao.baidu.com.如果你的项目在二级域名就写二级域名

前端url授权地址,在url中填写appid与你项目中方法中的oauth的地址,具体在下面的代码中可以看到.

  1. <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.(后面会有)

include('./Card/Common/class_weixin_adv.php');

之后填入微信官方给的的appid与secret

$weixin=new class_weixin_adv("appid", "secret");

初始化SDK的类,取到code,利用获取到的code在获取出openid 看下面代码注释!

  1. $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$_GET['code']."&grant_type=authorization_code"; 
  2. $res = $weixin->https_request($url);//调用SDK方法获取到res 从中可以得到openid 
  3. $res=(json_decode($res, true));//转换成array 方便调用openid 

继续调用SDK方法,获取到用户信息.此时$row已经获得用户信息了 可以var_dump下看看键值方便存入数据库

$row=$weixin->get_user_info($res['openid']);

获取用户信息就大功告成了,但这还不够.我们需要的是无需注册!所以需要利用openid,openid属于唯一凭证,每个用户对不同的公众号都有不同的openid.可以理解成用户账号的感觉.我这里用的是把openid存入cookie的解决方案,类似用户登陆的感觉,一些关键数据验证只需要与数据库中的openid进行对比.其他的一些利用方法可以发挥大家的想象!可以跟我留言交流!

关于之前的a链接的授权,大家也可以判断cookie是否存在openid,从而让未授权用户直接跳转到该地址,省却了用户的一步操作.

下面是完整逻辑代码,大家可以参考下!

  1. public function oauth2(){ 
  2.       include('./Card/Common/class_weixin_adv.php'); 
  3.        $weixin=new class_weixin_adv("appid", "secret"); 
  4.        if (isset($_GET['code'])){ 
  5.            $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$_GET['code']."&grant_type=authorization_code"; 
  6.            $res = $weixin->https_request($url); 
  7.            $res=(json_decode($res, true)); 
  8.            $row=$weixin->get_user_info($res['openid']); 
  9.             
  10.            if ($row['openid']) { 
  11.               //这里写上逻辑,存入cookie,数据库等操作 phpfensi.com
  12.  
  13.                cookie('weixin',$row['openid'],25920); 
  14.                
  15.            }else{ 
  16.                $this->error('授权出错,请重新授权!'); 
  17.            } 
  18.        }else{ 
  19.            echo "NO CODE"; 
  20.        } 
  21.        $this->display(); 
  22.    } 

SDK代码:微信官方有手册,我就不多讲了,自己研究,很简单的!.

  1. /** 
  2.  * 微信SDK  
  3.  * pan041ymail@gmail.com 
  4.  */ 
  5.  
  6. class class_weixin_adv 
  7. { 
  8.     var $appid = ""; 
  9.     var $appsecret = ""; 
  10.  
  11.     //构造函数,获取Access Token 
  12.     public function __construct($appid = NULL, $appsecret = NULL) 
  13.     { 
  14.         if($appid){ 
  15.             $this->appid = $appid; 
  16.         } 
  17.         if($appsecret){ 
  18.             $this->appsecret = $appsecret; 
  19.         } 
  20.  
  21.          
  22.         $this->lasttime = 1395049256; 
  23.         $this->access_token = "nRZvVpDU7LxcSi7GnG2LrUcmKbAECzRf0NyDBwKlng4nMPf88d34pkzdNcvhqm4clidLGAS18cN1RTSK60p49zIZY4aO13sF-eqsCs0xjlbad-lKVskk8T7gALQ5dIrgXbQQ_TAesSasjJ210vIqTQ"; 
  24.  
  25.         if (time() > ($this->lasttime + 7200)){ 
  26.             $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret; 
  27.             $res = $this->https_request($url); 
  28.             $result = json_decode($res, true); 
  29.              
  30.             $this->access_token = $result["access_token"]; 
  31.             $this->lasttime = time(); 
  32.         } 
  33.     } 
  34. //获取用户基本信息 
  35.     public function get_user_info($openid) 
  36.     { 
  37.         $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN"; 
  38.         $res = $this->https_request($url); 
  39.         return json_decode($res, true); 
  40.     } 
  41.  
  42. //https请求 
  43.     public function https_request($url, $data = null) 
  44.     { 
  45.         $curl = curl_init(); 
  46.         curl_setopt($curl, CURLOPT_URL, $url); 
  47.         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
  48.         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 
  49.         if (!emptyempty($data)){ 
  50.             curl_setopt($curl, CURLOPT_POST, 1); 
  51.             curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
  52.         } //phpfensi.com 
  53.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  54.         $output = curl_exec($curl); 
  55.         curl_close($curl); 
  56.         return $output; 
  57.     } 
  58. } 

Tags: PHP微信 一键登录

分享到: