微信登陆解析

工做上须要作个微信登陆,以前没有经验,本身摸索,结果遇到了一些坑,固然是解决了,可是有一个坑好无语,就是回调地址不能有http://,结果连接里的redirect_uri就要加上,否则就报参数错误。网上查找了好多redirect_uri参数错误的答案,居然没人提到这点,害我百思不解,我也是醉了。
php

首先是微信网页受权的官方文档,主要是依靠这个来完成微信登陆功能
html

网页受权数据库

首先要有一个服务号,有appid,appsecret,登陆公众平台后填好回调网址,有这三样就没什么问题了。json

下载一个微信开发者工具,成为开发者,会方便开发,固然没有也没什么问题,可是你就没法在电脑上直接观察调试结果了。api

这是登陆受权连接,只须要填写appid和redirect_uri。注意redirect_uri要加http://,固然也有多是我这个回调网址特殊。snsapi_userinfo是获取用户全部信息,具体参考官方说明,就是网页受权连接。微信

https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redire
cookie

不出意外点击这个连接会出现受权登陆页面,点登陆就会跳转到回调地址。微信开发

登陆后的地址相似:app

www.???.com/?code=???&state=1
微信公众平台

获得code能够获取access_token和opendi,code和access_token均可以当作令牌,opendi就是获得用户信息的关键。

插句话,要验证可否顺利获得access_token,也顺便验证你的appid和appsecret是否没问题,能够到这个网址验证,万一以后出问题能够排除appid和appsecret的错误。

微信公众平台接口调试工具

这是获取access_token的连接:

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=appsecret

直接上代码,不出问题能够获得用户信息:

<?php
class weixinclass
{
  var $appid;
  var $appsecret;
  //构造函数,获取Access Token
  public function __construct($appid = NULL, $appsecret = NULL)
  {
    if($appid){
      $this->appid = $appid;
    }
    if($appsecret){
      $this->appsecret = $appsecret;
    }
    $this->access_token = "";
      $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"];
  }
//获取用户基本信息
  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
header('content-type:text/html;charset=utf-8');
class OauthAction extends Action {
    public function oauth2(){
    require_once 'weixinclass.php';
      $appid = '填appid';
      $appsecret = '填appsecret ';
      $weixin=new weixinclass($appid,$appsecret);
      if (isset($_GET['code'])){
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&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,数据库等操做
            echo "<pre>";
            print_r($row);die;
          cookie('weixin',$row['openid'],25920);
        }else{
          $this->error('受权出错,请从新受权!');
        }
      }else{
        echo "NO CODE";
      }
      $this->display();
    }

}

上面的代码也是参考一些别人的内容,经测试没问题。而后打印出来的信息相似这样:

Array
(
    [subscribe] => 
    [openid] => 
    [nickname] => nobody
    [sex] => 1
    [language] => zh_CN
    [city] => 
    [province] => 
    [country] => 亚美尼亚
    [headimgurl] => 
    [unionid] => 
    [remark] => 
    [groupid] => 0
    [tagid_list] => Array
        (
        )

)

end.

相关文章
相关标签/搜索