html页面:
<html>
<head> <meta charset="utf-8" /> <title>第三方登录</title> <meta property="qc:admins" content="1541324001721762700063671645060454" /> </head> <body> <a href="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101213291&redirect_uri=http://wangbao.bwphp.cn/index.php&state=test"><img src="qq.png"/></a> </body></html>php代码:
<?php
header('Content-Type:text/html;charset=utf-8');//获取Authorization Code$url = 'https://graph.qq.com/oauth2.0/token?client_id=101213291' .'&client_secret=ed766c4ce0e44b4ba239f78765df226e' .'&redirect_uri=http://wangbao.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 openid$params = array();parse_str($info, $params);//把接收到的字符串转化为数组//print_r($params);die;$url1='https://graph.qq.com/oauth2.0/me?access_token='.$params['access_token']; // 传值token 得到callback响应函数$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数据以数组格式打印出//判断数据库是否存在$aa = mysql_connect('blog.bwphp.cn','wangbao','1234');$db = mysql_select_db('wangbao');$selSql = "select userid, token, openid, type from user_oauth where token= '".$params['access_token'] ."' and type = 'qq'";$selRe = mysql_query($selSql);$num = mysql_num_rows($selRe);if($num>0){ echo "欢迎再次登陆!";exit;}$addSql = "insert into user_oauth (token,openid,type) values ('".$params['access_token']."','".$arr['openid']."','qq')";$addRe= mysql_query($addSql);if($addRe){ echo "登陆成功!";exit;}echo "登陆失败!";exit;?>