微信第三方登录接口

微信第三方登录接口

 (2015-01-05 16:30:28)
 
      随着手机微信的崛起,腾讯发布的微信联登确实很诱惑pc端的伙伴们,现在就说说在pc端用微信扫一扫实现微信第三方登陆的方式。

   第一步:获取AppID  AppSecret (不做解释,自己去微信公众平台申请)

第二步:生成扫描二维码,获取code
https://open.weixin.qq.com/connect/qrconnect?appid=AppID&redirect_uri=http://www.baidu.com&response_type=code&scope=snsapi_login&state=2014#wechat_redirect

第三步:通过code获取access_token
https://api.weixin.qq.com/sns/oauth2/access_token?appid=AppID&secret=AppSecret&code=00294221aeb06261d5966&grant_type=authorization_code
第四步:因接口频率有次数限制,如果需要,刷新access_token
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=AppID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN

     第步:通过access_token和openid获取用户的基础信息,包括头像、昵称、性别、地区
https://api.weixin.qq.com/sns/userinfo?access_token=bezXEiiBSKSxW0eoblIewFNHqAG-PyW9OqI_L81E4ZCi2cFpfoJTyQc0xKlPPCtqK1kLJfiRbVrpoOVLw7fjpqh52bn7C68SHa2HSgYsVPXZPvJvtayDa57-_7TeHYw&openid=o39YsbmuV_bIPGpj1MTe


这是接口在线调试工具:
http://mp.weixin.qq.com/debug/
这是错误码说明文档:
http://mp.weixin.qq.com/wiki/17/fa4e1434e57290788bde25603fa2fcbd.html 
另外需要特别注意,如果开发者有在多个公众号,或在公众号、移动应用之间统一用户帐号的需求,需要前往微信开放平台(open.weixin.qq.com)绑定公众号后,才可利用UnionID机制来满足上述需求,文档:
http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html

嗯~ 嗯~ 也不是很难·· 祝各位顺利!
 
 
<?php 
/***************************/
/* Wechat 登录              /
/* by tiandi 2014.12.6      /
/***************************/ 
 
if (defined('WEBSITE') || defined('GETINFO'))
{
    global $_LANG;
    $_LANG['help']['APP_KEY'] = '在微信开发者平台申请的AppID';
    $_LANG['help']['APP_SECRET'] = '在微信开发者平台申请的AppSecret';
     
    $_LANG['APP_KEY'] = 'AppID';
    $_LANG['APP_SECRET'] = 'AppSecret';
     
    $i = isset($web) ? count($web) : 0;
    // 类名
    $web[$i]['name'] = 'wechat';
    // 文件名,不包含后缀
    $web[$i]['type'] = 'wechat';
     
    $web[$i]['className'] = 'wechat';
     
    // 作者信息
    $web[$i]['author'] = 'tiandi';
     
    // 作者QQ
    $web[$i]['qq'] = '';
     
    // 作者邮箱
    $web[$i]['email'] = '';
     
    // 申请网址
    $web[$i]['website'] = 'http://open.weixin.qq.com';
     
    // 版本号
    $web[$i]['version'] = '1.0';
     
    // 更新日期
    $web[$i]['date']  = '2014-12-6';
     
    // 配置信息
    $web[$i]['config'] = array(
        array('type'=>'text' , 'name'=>'APP_KEY', 'value'=>''),
        array('type'=>'text' , 'name' => 'APP_SECRET' , 'value' => ''),
    );
}
 
 
if (!defined('WEBSITE'))
{
    include_once(dirname(__FILE__).'/oath2.class.php');
    class website extends oath2
    {
        function website()
        {
            $this->app_key = APP_KEY;
            $this->app_secret = APP_SECRET;
             
            $this->scope = 'snsapi_login';
            //by tiandi authorizeURL是用来PHP打开微信登录时用,JS调用则不用authorizeURL。
            $this->authorizeURL = 'https://open.weixin.qq.com/connect/qrconnect';
 
            $this->tokenURL = 'https://api.weixin.qq.com/sns/oauth2/access_token';
            $this->refreshtokenURL = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';
            $this->userURL = 'https://api.weixin.qq.com/sns/userinfo';
            $this->meth = 'GET';
        }
 
        function Code2Token($code)
        {
            $params  = 'appid='.$this->app_key.'&secret='.$this->app_secret.'&code='.$code.
                '&grant_type=authorization_code';
            $tokenurl = $this->tokenURL."?". $params;
            $token = $this->http($tokenurl, 'GET');
            $token = json_decode($token , true);
            return $token;
        }
 
        function GetRefreshToken($token)
        {
            $params  = 'appid='.$this->app_key.'&grant_type=refresh_token&refresh_token='.$token;
            $tokenurl = $this->refreshtokenURL."?". $params;
            $token = $this->http($tokenurl, 'GET');
            $token = json_decode($token , true);
            return $token;
        }
         
        function Getinfo($token,$openid)
        {
            $params = 'access_token='.$token.'&openid='.$openid;
            $userurl = $this->userURL."?". $params;
            $userinfo = $this->http($userurl, 'GET');
            return json_decode($userinfo , true);
        }
    }
}