网页版 QQ授权登录

官方文档:

https://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0

第一步:

$app_id = Config::get('custom.qqAppId');
//回调地址
$my_url = 'http://'.$_SERVER['HTTP_HOST'].'/store/getToken';
$redirect_uri = urlencode($my_url);
//state参数用于防止CSRF攻击,成功授权后回调时会原样带回
//拼接URL
$url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" . $app_id . "&redirect_uri=" . $redirect_uri . "&state=dsjhqq";
header("Location:" . $url);

第二步:

$app_id = Config::get('custom.qqAppId');
$app_secret = Config::get('custom.qqAppKey');
//回调地址
$redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].'/store/getWxToken';
$redirect_uri = urlencode($redirect_uri);
/*
* NO.2:获取 access_token
*/
$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=" . $app_id . "&client_secret=" . $app_secret ."&code=" . $code . "&redirect_uri=" . $redirect_uri;
$response = file_get_contents($token_url);
//把传回来的数据参数变量化
$params = array();
parse_str($response, $params);
/*
* NO.3:获取openId
*/
$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];
$str = file_get_contents($graph_url);
$user = json_decode($str);//存放返回的数据 client_id ,openid

/*
* NO.4:使用 access_token 和 openid 来获取用户信息。
*/
$user_data_url = "https://graph.qq.com/user/get_user_info?access_token=" . $params['access_token'] . "&oauth_consumer_key=" . $app_id . "&openid=" . $user->openid . "&format=json";
$user_data = file_get_contents($user_data_url);//此为获取到的user信息
$user_data = json_decode($user_data, true);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值