• php实现qq登录


    <?php
    /**
    * Project:QQ授权登陆
    * User: luokakale
    * Date: 2019/1/25
    * Time: 14:22
    */
    
    //应用的APPID
    $app_id = "";
    //应用的APPKEY
    $app_secret = "";
    //【成功授权】后的回调地址,即此地址在腾讯的信息中有储存
    $my_url = "http://XXXXXXX/login.php";
    
    /*
    * No1:获取Authorization Code
    */
    
    session_start();
    
    if(empty($code))
    {
    //state参数用于防止CSRF攻击,成功授权后回调时会原样带回
    $_SESSION['qq_state'] = md5(uniqid(rand(), TRUE));
    //拼接URL
    $dialog_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=" . $_SESSION['qq_state'];
    echo("<script> top.location.href='" . $dialog_url . "'</script>");
    }
    
    //获取qq回调时返回的code参数
    $code = $_REQUEST["code"];//存放Authorization Code
    
    /*
    * NO.2 通过Authorization Code获取Access Token
    */
    if($_REQUEST['state'] == $_SESSION['qq_state'] ) {
    //拼接URL获取access_token
    $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"."client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)."&client_secret=" . $app_secret . "&code=" . $code;
    $response = file_get_contents($token_url);
    }
    
    /*
    * NO.3:获取用户openID
    */
    
    //把传回来的数据参数变量化
    $params = array();
    parse_str($response, $params);
    $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来获取所接受的用户信息。
    */
    $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);
  • 相关阅读:
    mysql数据库之多表查询
    mysql数据库之单表查询
    mysql数据库之表和库
    mysql数据库之windows版本
    mysql数据库之linux版本
    mysql数据库之mysql下载与设置
    python基础之逻辑题(3)
    python基础之逻辑题(2)
    python基础之逻辑题(1)
    spring boot统一异常处理
  • 原文地址:https://www.cnblogs.com/zyfeng/p/12095167.html
Copyright © 2020-2023  润新知