当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP版微信第三方实现一键登录及获取用户信息的方法

PHP版微信第三方实现一键登录及获取用户信息的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了php版微信第三方实现一键登录及获取用户信息的方法。分享给大家供大家参考,具体如下:

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

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

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

复制代码 代码如下:
<a href="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 看下面代码注释!

$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下看看键值方便存入数据库

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

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

关于之前的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'],25920);
    }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;
  }
}

更多关于php相关内容感兴趣的读者可查看本站专题:《php微信开发技巧汇总》、《php编码与转码操作技巧汇总》、《php网络编程技巧总结》、《php基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网