1.在腾讯开放平台中注册应用 平台会给一个
<meta property="qc:admins" content="066705620216056762700063671645060454" />
//获取Authorization Code client_id client_secret 区分不同用户的钥匙
//回调
$url = 'https://graph.qq.com/oauth2.0/token?client_id=101213361'
.'&client_secret=7e23e11abfe567125b4b12984977d75b'
.'&redirect_uri=http://hejinpeng.bwphp.cn/index.php'
.'&grant_type=authorization_code'
.'&code='.$_REQUEST['code'];//访问https://graph.qq.com/oauth2.0/token,传值APPID,APPKEY,并接收到Authorization Code
$info = file_get_contents($url);//得到Access Token//access_token 授权
print_r($info)."</br>";
$params = array();
parse_str($info, $params);//把接收到的字符串转化为数组
//print_r($params['access_token'])."</br>";//die; $params['access_token']为接收到的token值
$url1='https://graph.qq.com/oauth2.0/me?access_token='.$params['access_token'];
$open=file_get_contents($url1);//访问https://graph.qq.com/oauth2.0/me?access_token 传值token 得到callback响应函数
//print_r($open);"<br/>";
$str1 = substr($open,9,-3);//将得到的字符串截串为json格式数据
$arr= json_decode($str1, true);//解析json数据,true设置可以使json数据以数组格式打印出
//print_r($arr['openid']);echo "<br/>"; //$arr['openid']为openid
//die;
$url2="https://graph.qq.com/user/get_user_info?access_token=".$params['access_token']."&oauth_consumer_key=101213361&openid=".$arr['openid'];//访问https://graph.qq.com/user/get_user_info 传值token,APPID,openid
//print_r($url2); echo "<br/>";
$data=file_get_contents($url2);//抓取用户信息数据 返回json格式数据
//print_r($data);die;
/*以下为入库*/
//判断数据库是否存在
// $db = new PDO("mysql:host=localhost;dbname=root","root", "root");
$aa = @mysql_connect('hejinpeng.bwphp.cn','hejinpeng','1234');
$db = mysql_select_db('hejinpeng');
$selSql = "select userid, token, openid, type from oauth where token= '".$params['access_token'] ."' and type = 'qq'";
//print_r($selSql);die;
// $selRe = $db->query($selSql);
$selRe = mysql_query($selSql);
$num = mysql_num_rows($selRe);
// $num = $selRe->rowCount();
if($num>0)
{
echo "欢迎再次登陆!";exit;
}
// $addRe= $db->exec($addSql);
// print_r($addRe);die;
// $addRe= mysql_query($addSql);
$addSql = "insert into oauth (token,openid,type) values ('".$params['access_token']."','".$arr['openid']."','qq')";
$addRe= mysql_query($addSql);
if($addRe)
{
echo "登陆成功!";exit;
}
echo "登陆失败!";exit;
复制到index.html中放到外网中进行验证
2.验证成功以后会得到:
App Key : 101213361
App Secret : 3bd600efbe3a1cd85f477111af410550
3. index.html 中的代码:
<html>
<header>
<meta charset='utf-8'></meta>
<meta property="qc:admins" content="066705620216056762700063671645060454" /></header>
<a href="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101213361&redirect_uri=http://hejinpeng.bwphp.cn/index.php&state=test"><img src="./qq.png" alt="" /></a>
</html>
<header>
<meta charset='utf-8'></meta>
<meta property="qc:admins" content="066705620216056762700063671645060454" /></header>
<a href="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101213361&redirect_uri=http://hejinpeng.bwphp.cn/index.php&state=test"><img src="./qq.png" alt="" /></a>
</html>
将id="" 改为自己的App Key
redirect_uri 为自己的外网域名 传参数 目标跳转index.php
4.index.php 中代码:
<?php
header("content-type:text/html;charset=utf-8");
//判断数据库是否存在
// $db = new PDO("mysql:host=localhost;dbname=root","root", "root");
// $addRe= $db->exec($addSql);
echo "登陆成功!";exit;
echo "登陆失败!";exit;