QQ authorized login based on php

Step one:

First log in QQ Internet Homepage https://connect.qq.com/ Conduct personal / business certification. The audit time is about one week.

Create an application after the certification:

The APP ID and APP Key parameters of the application are mainly used here.

To fill in the callback address is to request the qq interface to get the code parameter callback to your address

The second step:

After the preparation work is completed, you can access qq authorization. Here you can directly enter the demo code:

<?php
/**
 * Project:QQ Authorized landing
 * User: luokakale
 * Date: 2019/1/25
 * Time: 14:22
 */

//Applied APPID
$app_id = "";
//Applied APPKEY
$app_secret = "";
//[The callback address after successful authorization, that is, this address is stored in Tencent's information
$my_url = "http://XXXXXXX/login.php";

/*
 * No1: Get Authorization Code
 */

session_start();

if(empty($code))
{
    //state Parameter to prevent CSRF Attack. After successful authorization, the callback will bring back as is
    $_SESSION['qq_state'] = md5(uniqid(rand(), TRUE));
    //Splicing 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>");
}

//Obtain qq Returned on callback code parameter
$code = $_REQUEST["code"];//Deposit Authorization Code

/*
 * NO.2 Get Access Token through Authorization Code
 */
if($_REQUEST['state'] == $_SESSION['qq_state'] ) {
    //Splicing URL Obtain 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:Get user openID
 */

//Variable the parameters of the returned data
$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);//Store returned data client_id ,openid

/*
 * NO.4: Use access? Token to get the accepted user information.
 */
$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);//This is the obtained user information
$user_data = json_decode($user_data, true);

After getting the user information, we can make a series of logical judgments. It's that simple.

 

This is the original content. In order to respect the work of others, please indicate the address of this article:

https://www.cnblogs.com/luokakale/p/10319488.html 

Keywords: PHP JSON

Added by Phairest on Wed, 04 Dec 2019 22:45:23 +0200