QQ登录功能实现

原因:公司的注册与登录不再像以前那样子使用自定义的账号密码登录了,要改成方便的微信扫码登录与QQ登录了

教程开始

需要用到的网址:

  1. https://connect.qq.com/index.html QQ互联网站

  2. http://wiki.connect.qq.com/%E4%BD%BF%E7%94%A8authorization_code%E8%8E%B7%E5%8F%96access_token API文档

准备工作:

1. 登录你的QQ互联后台

2. 创建网站应用

3. 弄个开发者认证吧,没有开发者认证啥都做不了

4. 应用基本信息

5. 填写网络地址(网站域名),填写网站回调域,这里的网站回调域就是具体到QQ登录功能的地址(TP5 例: 域名/模块/控制器/方法)

6. 查看应用接口中有没有 get_user_info(API)接口

正式开发

1. 发送获取CODE参数请求,浏览器打开网址

https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=appid&redirect_uri=redirect_uri

参数 是否必须 说明
response_type code 固定参数
client_id APPID 创建应用后所获得
redirect_uri 网站回调域

2. 获取CODE参数

参数 说明
code 获取ACCESS_TOKEN需要的参数

3. 发送获取ACCESS_TOKEN的请求

这里需要写CURL方法获取数据

//php curl(GET)请求
public function curlGet($url){
    if(empty($url)){
        return false;
    }
    $output = '';

    $ch = curl_init();
    $str =$url;
    curl_setopt($ch, CURLOPT_URL, $str);
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    $output = curl_exec($ch);
    return $output;
}

https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=appid&client_secret=appkey&code=$codeInfo[code]&redirect_uri=redirect_uri

参数 是否必须 说明
grant_type refresh_token 固定参数
client_id APPID
client_secret APPKEY(创建应用所产生的APPKEY)
code 上一步所获取的code
redirect_uri 网站回调域

4. 获取ACCESS_TOKEN参数

这里腾讯返回的数据是一个字符串需要我们写一个自定义函数来获取参数,以下是本人写的一个方法

/**
 * @name 解析地址栏参数
 * @auth Sam
 * @param $str
 * @return array|bool
 */
public function formateUrlParam($str)
{
    if(empty($str) || !is_string($str)){
        return false;
    }

    $arr = explode('&',$str);

    $keyValue = array();
    $newArr = array();
    foreach($arr as $k=>$v){
        $keyValue = explode('=',$v);

        $newArr[$keyValue[0]] = $keyValue[1];

        $keyValue = array();

    }

    return $newArr;
}
参数 说明
access_token 授权令牌,Access_Token
expires_in 该access token的有效期,单位为秒
refresh_token 在授权自动续期步骤中,获取新的Access_Token时需要提供的参数

5. 发送获取OPENID请求

https://graph.qq.com/oauth2.0/me?access_token=access_token

参数 是否必须 说明
access_token access_token

6. 获取OPENID参数

这里返回的数据也是一串字符串,但与上面格式又不同,所以需要自己自定义函数获取参数

//获得用户openID
$openId = $this->curlGet("https://graph.qq.com/oauth2.0/me?access_token=$accToken[access_token]");

$openId = trim($openId,"callback( ");

$openId = explode(')',$openId); 
$openId = trim($openId[0],' ');


$openId = json_decode($openId,true);

7. 发送获取用户信息请求

https://graph.qq.com/user/get_user_info?access_token=access_token&oauth_consumer_key=appid&openid=openid

参数 是否必须 说明
access_token access_token
oauth_consumer_key APPID
openid OPENID

8. 获取用户信息

参数 说明
ret 返回码
msg 对应错误信息
is_lost 是否丢失
nickname QQ呢称
gender 性别
province 省份
city 城市
year 年份
figureurl 30X30的头像图片地址
figureurl_1 50X50的头像图片地址
figureurl_2 100X100的头像图片地址
figureurl_qq_1 40X40的头像图片地址
figureurl_qq_2 100X100的头像图片地址
is_yellow_vip 黄钻用户
vip 黄钻用户
yellow_vip_level 黄钻等级
level 黄钻等级
is_yellow_year_vip 是否为年费黄钻用户

结语

坑爹腾讯,两次返回的数据格式都不同,每次都要自己写函数,觉得做API开发最好的就是阿里了,几乎没有难度

原链接:

https://my.oschina.net/u/3554366/blog/1507092

猜你喜欢

转载自blog.csdn.net/weixin_39662805/article/details/82221268