PHP QQ Login

Source: Internet
Author: User
Tags openid

Place button:
such as in the homepage index.php the following code (that is, the drop button)
<a href= "qq/index.php" &GT;QQ login </a>

Then jump to the qq/index.php file with the following code:
$url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=[your_appid]&redirect_uri=[ Your_redirect_uri]&scope=[the_scope] "
Header ("Location:".) $url);

Then jumps to the value specified in Redirect_uri=[your_redirect_uri] [Your_redirect_uri], such as qq/callback.php
With a value of code=value.

The qq/callback.php code is as follows:

$code = $_get[' code ');
$url = "Https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=[your_appid]&client_ Secret=[app key]&code= ". $code. " &state=lin3615&redirect_uri= ". UrlEncode ("http://www.xxxx.net/qq/callback.php");
Get_contents ($url) used to get the returned data
function Get_contents ($url) {
if (Ini_get ("allow_url_fopen") = = "1") {
$response = file_get_contents ($url);
}else{
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
curl_setopt ($ch, Curlopt_returntransfer, TRUE);
curl_setopt ($ch, Curlopt_url, $url);
$response = curl_exec ($ch);
Curl_close ($ch);
}

return $response;
}
$response = Get_contents ($url);
Parse the data with $access_token values
Parse_str ($response);
Callback ($STR) Convert JSON format to array form
function Callback ($STR)
{
Return Json_decode ($str, true);
}
$url = "https://graph.qq.com/oauth2.0/me?access_token=". $access _token;
$callback = Get_contents ($url);
$callback = Str_replace ("(", "('", $callback);
$callback = Str_replace (")", "')", $callback);
Eval (' $data = '. $callback);

Openid
$openid = $data [' OpenID '];

$url = "https://graph.qq.com/user/get_user_info?access_token= $access _token&oauth_consumer_key=[your_appid] &openid= ". $openid;

$DD = Get_contents ($url);
$DD = Json_decode ($dd, true);
Print_r ($DD);
Where $DD is the related QQ information array

Connect to the database as follows;
If the following data table
User: Users table, normal non-QQ login, fields are:
UID Self-increment
Name User name
Password Password
.....

QQCONNECT:QQ Connection table, QQ connection when used, the fields are:
Quid uid corresponds to UID in user table
OpenID $opendid
Access_token $access _token
......

Connecting to a database
$link = mysql_connect ();
$sql = "Select quid from Qqconnect where openid= ' {$openid} ' and Access_token = ' {$access _token} '";
$res = mysql_query ($sql, $link);
If it already exists,
if ($res && mysql_num_rows ($res))
{
$row = Mysql_fetch_assoc ($res);
$uid = $row [' Quid '];
$query = "SELECT * from user where uid = ' {$uid} '";
$RR = mysql_query ($query, $link);
$rrs = Mysql_fetch_assoc ($RR);
$_session[' uid '] = $rrs [' uid '];
$_session[' name '] = $rrs [' name '];
Header ("Location:".) Home);
}else
{
Does not exist, the binding form is displayed directly
echo "<from>";
Echo ';
echo "</from>";
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.