php验证qq,php QQ第三方登录/OAuth2.0验证

qq实现第三方网站登录

创建QQ互联账号

可用QQ号码登录 登录地址:http://connect.qq.com/

QQ互联->管理中心->创建应用

1.创建应用

0818b9ca8b590ca3270a3433284dd417.png

2.创建网站

0818b9ca8b590ca3270a3433284dd417.png

注意内容:网站地址一定要加入图中所标注的信息

创建成功

获取 APP ID 和  APP KEY

header请求

public function actionQQ()

{

//$redirect 为回调地址 $app_id 应用编号

$url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=' . $app_id . '&redirect_uri=' . $redirect;

header('Location:' . $url);

}

获取QQ用户信息

class QQ_sdk

{

private $app_id = '101330572';

private $app_secret = 'e5526467a164e344f7842c3682132366';

private $redirect = 'http://www.ukagaka.cn/';

function __construct()

{

}

/**

* [get_open_id 获取用户唯一ID,openid]

* @param [string] $token [授权码]

* @return [array] [成功返回client_id 和 openid ;失败返回error 和 error_msg]

*/

function get_open_id($token)

{

$str = $this->curl_get_content('https://graph.qq.com/oauth2.0/me?access_token=' . $token);

if (strpos($str, "callback") !== false) {

$lpos = strpos($str, "(");

$rpos = strrpos($str, ")");

$str = substr($str, $lpos + 1, $rpos - $lpos - 1);

}

$user = json_decode($str, TRUE);

return $user;

}

/**

* [get_access_token 获取access_token]

* @param [string] $code [登陆后返回的$_GET['code']]

* @return [array] [expires_in 为有效时间 , access_token 为授权码 ; 失败返回 error , error_description ]

*/

function get_access_token($code)

{

$token_url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&'

. 'client_id=' . $this->app_id . '&redirect_uri=' . urlencode($this->redirect) . '&client_secret=' . $this->app_secret . '&code=' . $code;

$token = array();

parse_str($this->curl_get_content($token_url), $token);

return $token;

}

/**

* [get_user_info 获取用户信息]

* @param [string] $token [授权码]

* @param [string] $open_id [用户唯一ID]

* @return [array] [ret:返回码,为0时成功。msg为错误信息,正确返回时为空。...params]

*/

function get_user_info($token, $open_id)

{

$user_info_url = 'https://graph.qq.com/user/get_user_info?' . 'access_token=' . $token . '&oauth_consumer_key=' . $this->app_id . '&openid=' . $open_id . '&format=json';

$info = json_decode($this->curl_get_content($user_info_url), TRUE);

return $info;

}

private function curl_get_content($url)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_URL, $url);

//设置超时时间为3s

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

$result = curl_exec($ch);

curl_close($ch);

return $result;

}

}

可在设置目录下测试

if(isset($_GET['code'])) {

//qq获取信息

include('QQ_sdk.php');

$qq_sdk = new Qq_sdk();

$token = $qq_sdk->get_access_token($_GET['code']);

echo "

";

print_r($token)."";

echo "

";

$open_id = $qq_sdk->get_open_id($token['access_token']);

echo "

";

print_r($open_id)."";

echo "

";

}

?>

获取用户信息

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值