PHP

自己写的qq登录功能,无需官方SDK

字号+ 编辑: 国内TP粉 修订: 听风就是我 来源: ThinkPHP 2023-09-12 我要说两句(0)

由于腾讯qq互联官方的SDK太麻烦,所以自己写了一个自己用!

主要用到的文件有

1.自定义异常处理类  HttpException 位于application/lib/exception/HttpException .php

2.配置文件 config/qq.php

3.自己写的核心文件 Qq.php


TP版本V5.1.5

PHP 版本 7.2

对于自定义异常处理类做了很多处理 所以这里就不发了,大家用的时候可以自己扩展一下,也可以参考官方文档自定义一个异常处理

对于配置文件中的重要输入 会用*代替

没有对用户注销做处理,大家可以自己写一下,在用户退出的时候将调用qq登录接口的Session全部删掉就可以了!


Qq.php


<?php
/**
 * Email: 1614369925@qq.com
 * User: 李昊天
 * Date: 2018/3/4
 * Time: 10:36
 */
namespace app\user\controller;
use app\lib\exception\HttpException;
use think\facade\Session;
class Qq
{
    private $get_code_url = ''; //获取code的地址
    private $appId = '';    //appid
    private $appKey = '';    //appkey
    private $callback = '';  //回调地址
    public function __construct()
    {
        $this->appId = config('qq.appId');
        $this->appKey = config('qq.appKey');
        $this->callback = config('qq.callback');
    }
    public function qqLogin()
    {
        Session::set('qAuthData.state', md5(uniqid(rand(), TRUE)));
        $this->get_code_url = sprintf(config('qq.qq_login.get_code_url'), $this->appId, $this->callback, Session::get('qAuthData.state'), 'all');  //获取Code 地址
        return $this->get_code_url;
    }
    public function qq_callback()
    {
        if (Session::get('qAuthData.state') != input('get.state')) {
            throw new HttpException([
                'msg' => '参数错误,请重新发起登录请求!'
            ]);
        }
        $code = input('get.code');
        Session::set('qAuthData.code', $code);
        $url = sprintf(config('qq.get_account_token.get_account_token_url'), $this->appId, $this->callback, $this->appKey, $code);
        $Result = curl_get($url);
        //$response ='callback( {"error":100020,"error_description":"code is reused error"} )';  //请求失败示例
        //$response = 'access_token=************&expires_in=7776000&refresh_token=************';  //请求成功示例
        if (strpos($Result, "callback") !== false) {
            $lpos = strpos($Result, "(");
            $rpos = strrpos($Result, ")");
            $response = substr($Result, $lpos + 1, $rpos - $lpos - 1);
            $error = json_decode($response );
            throw new HttpException([
                'msg' => $error->error_description,
                'errorCode' => $error->error
            ]);
        }
        $data = [];
        parse_str($Result, $data);
        if (!array_key_exists('access_token', $data)) {
            throw new HttpException([
                'msg' => '获取access_token失败,服务器错误!',
                'errorCode' => '500'
            ]);
        }
        $access_token = $data['access_token'];
        Session::set('qAuthData.access_token', $access_token);
        return $data;
    }
    public function get_openid()
    {
        $get_openId_url = config('qq.get_openId.get_openId_url') . '?access_token=' . Session::get('qAuthData.access_token');
        $result = curl_get($get_openId_url);
        //$result = 'callback( {"client_id":"******","openid":"*************"} )'; //请求成功示例
        //$result = callback( {"error":100007,"error_description":"param access token is wrong or lost "} ) //请求失败示例
        #检测错误是否发生
        if (strpos($result, "callback") !== false) {
            $l = strpos($result, "(");
            $r = strrpos($result, ")");
            $Result = substr($result, $l + 1, $r - $l - 1);
        }
        $user = json_decode($Result);
        Session::set('qAuthData.openId', $user->openid);
        return $user->openid;
    }
    public function userinfo()
    {
        $UserData = Session::get('qAuthData');
        if (!array_key_exists('access_token', $UserData)) {
            throw new HttpException([
                'msg' => 'access_token 不存在 无法获取用户信息!'
            ]);
        }
        if (!array_key_exists('openId', $UserData)) {
            throw new HttpException([
                'msg' => '用户openID不存在,请重新登录!'
            ]);
        }
        $access_token = $UserData['access_token'];
        $openId = $UserData['openId'];
        $get_user_info = sprintf(config('qq.get_user_info.get_user_info_url'), $access_token, $openId, $this->appId);
        $UserResult = curl_get($get_user_info);
        $res = json_decode($UserResult);
        if(!property_exists($res,'ret')){
            throw new HttpException([
                'msg' => '获取用户信息失败,未知错误'
            ]);
        }
        if($res->ret != 0){
            throw new HttpException([
                'msg' => $res->msg
            ]);
        }
        return json($res);
    }
}



config/qq.php 文件


<?php
/**
 * User: 李昊天
 * Email: 1614369925@qq.com
 * Date: 2018/2/26
 * Time: 13:57
 */
return [
    'appId' => '*********',
    'appKey' => '*********',
    'callback' => '*********',
    'qq_login' => [
        'get_code_url' => 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s&scope=%s'
    ],
    'get_account_token'=>[
        'grant_type' => 'authorization_code',
        'get_account_token_url' => 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=%s&redirect_uri=%s&client_secret=%s&code=%s'
    ],
    'get_openId' => [
        'get_openId_url'=>'https://graph.qq.com/oauth2.0/me'
    ],
    'get_user_info' => [
        'get_user_info_url' => 'https://graph.qq.com/user/get_user_info?format=json&access_token=%s&openid=%s&oauth_consumer_key=%s'
    ],
];


阅完此文,您的感想如何?
  • 有用

    0

  • 没用

    0

  • 开心

    0

  • 愤怒

    0

  • 可怜

    0

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.交流群: PHP+JS聊天群

相关课文
  • mac开发接入微信公众号接口返回报错 cURL error 56: SSLRead() return error -9806

  • pecl安装程序时报错Array and string offset access syntax with curly braces is no longer supported

  • PHP的换行符是什么

  • 由于商家传入的H5交易参数有误,该笔交易暂时无法完成,请联系商家解决

我要说说
网上宾友点评