Home  >  Article  >  Backend Development  >  Detailed explanation of using PHP to implement QQ third-party login

Detailed explanation of using PHP to implement QQ third-party login

PHPz
PHPzOriginal
2023-04-03 11:49:50962browse

In today's Internet environment, third-party login has become standard for websites or apps, including QQ third-party login. And how to use PHP to implement QQ third-party login? Let’s introduce the process in detail below.

First of all, you need to understand the QQ Internet open platform. QQ Internet provides a series of APIs, including website access, mobile access and application access. What we need to use here is website access.

Step one: Apply for QQ Internet application

You need to go to the official website of QQ Internet Open Platform, log in to your QQ account, create an application and obtain the app ID and app key.

Step 2: Guide the user to the QQ Internet login page

Users need to log in with their own QQ account, enter the QQ Internet homepage, and authorize our website to log in.

Step Three: Obtain access_token and openid

After the user is authorized to log in successfully, QQ Internet will call back the callback url we filled in on the QQ Internet platform and pass us a code parameter through GET. , using this code parameter, we can obtain the access_token and openid by requesting the access_token interface of QQ Internet.

The code is as follows:

$appid = ''; //应用ID
$appkey = ''; //应用Key
$callback = ''; //回调地址

$code = $_GET['code'];

$get_token_url='https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id='.$appid.'&client_secret='.$appkey.'&code='.$code.'&redirect_uri='.$callback.'';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $get_token_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$access_token = explode('&',curl_exec($ch))[0];

$access_token = str_replace('access_token=','',$access_token);

$get_openid_url = 'https://graph.qq.com/oauth2.0/me?access_token='.$access_token;

curl_setopt($ch, CURLOPT_URL, $get_openid_url);

$openid = trim(curl_exec($ch),'callback()');

$openid = json_decode($openid,true);

$openid = $openid['openid'];

Step 4: Obtain user information

After obtaining access_token and openid, we can use them to request the API interface to obtain user information. It is worth noting here that the appid and openid parameters need to be added to the request link to obtain the correct user information.

The code is as follows:

$get_user_info_url='https://graph.qq.com/user/get_user_info?'.'access_token='.$access_token.'&oauth_consumer_key='.$appid.'&openid='.$openid.'&format=json';

curl_setopt($ch, CURLOPT_URL, $get_user_info_url);

$qq_user_info = json_decode(curl_exec($ch),true);

Step 5: Process user information

Finally, we need to process the user information obtained from QQ Internet, such as openid and user information is stored in the database for future use.

If you need other interfaces, you can go to the QQ Internet Open Platform to view relevant documents and develop according to actual needs.

The above is the process of realizing QQ third-party login through PHP. I hope to be helpful!

The above is the detailed content of Detailed explanation of using PHP to implement QQ third-party login. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn