参考文档链接:
http://wiki.open.qq.com/wiki/website/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_OAuth2.0
文件名为:teng.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <meta property="qc:admins" content="142144174167310116270006367" /> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta property="qc:admins" content="142144174167310116270006367" /> </head> <body> <a href="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101218540&redirect_uri=http://zhangsixia.bwphp.cn/tengxun.php&state=test"><img src="qq.png"/></a> </body> </html>
php文件名:
<?php header(‘Content-Type:text/html;charset=utf-8‘); //getToken $url = ‘https://graph.qq.com/oauth2.0/token?client_id=101218540‘ .‘&client_secret=034ab318017483c1c2c90cee1fa2dee9‘ .‘&redirect_uri=http://zhangsixia.bwphp.cn/tengxun.php‘ .‘&grant_type=authorization_code‘ .‘&code=‘.$_REQUEST[‘code‘]; $info = file_get_contents($url); $params = array(); //parse_str() 函数把查询字符串解析到变量中 parse_str($info, $params); //print_r($params);die; //获取openid $url1 = "https://graph.qq.com/oauth2.0/me?access_token=".$params[‘access_token‘].""; $info1 = file_get_contents($url1); //print_r($info1);die; $str = substr($info1,9,-3); //print_r($str);die; $arr = json_decode($str,true); //print_r($arr[‘openid‘]);die; //获取用户信息 $url2 = "https://graph.qq.com/user/get_user_info?access_token=".$params[‘access_token‘]."&oauth_consumer_key=101218540&openid=".$arr[‘openid‘].""; $info2 = file_get_contents($url2); $data = json_decode($info2,true); //print_r($data); //判断数据库是否存在 @$aa = mysql_connect(‘‘,‘‘,‘‘); $db = mysql_select_db(‘‘); @$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); //mysql_num_rows() 函数返回结果集中行的数目 if($num>0) { echo "欢迎再次登陆!";exit; } $addSql = "insert into user_oauth (token,openid,type) values (‘".$params[‘access_token‘]."‘,‘".$arr[‘openid‘]."‘,‘qq‘)"; //echo $addSql;die; $addRe= mysql_query($addSql); if($addRe) { echo "登陆成功!"; print_r($data); exit; } echo "登陆失败!";exit; ?>
时间: 2024-10-09 23:39:38