标签:第三方
一、qq的第三方认证
打开qq互联 http://connect.qq.com/ ,创建应用
创建成功后会有 App Id 和App Key
下面我们就可以添加我们的网站地址,然后进行认证,并输入回调地址,这样我们的qq第三方就配置完成了。代码实现如下,可供参考
html代码如下
<meta property="qc:admins" content="160022761067251627000636" /> <html> <head> <meta charset="utf-8" /> <title>第三方登录</title> </head> <body> <a href="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101197633&redirect_uri=http://zhangrui.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=101197633‘
.‘&client_secret=6f0d2d83bdf8e43b96e468e9ca5dd6ca‘
.‘&redirect_uri=http://zhangrui.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
// print_r($info);die;
$params = array();
parse_str($info, $params);//把接收到的字符串转化为数组
//print_r($params[‘access_token‘]);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格式数据
//print_r($str1);die;
$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=101197633&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);
/*以下为入库*/
//判断数据库是否存在
// $db = new PDO("mysql:host=localhost;dbname=zhangrui","zhangrui", "123456");
$aa = @mysql_connect(‘localhost‘,‘zhangrui‘,‘1234‘);
$db = mysql_select_db(‘zhangrui‘);
$selSql = "select userid, token, openid, type from user_oauth where token= ‘".$params[‘access_token‘] ."‘ and type = ‘qq‘";
// $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 user_oauth (token,openid,type) values (‘".$params[‘access_token‘]."‘,‘".$arr[‘openid‘]."‘,‘qq‘)";
$addRe= mysql_query($addSql);
if($addRe)
{
echo "登陆成功!";exit;
}
echo "登陆失败!";exit;这样我们就可以实现简单的登录并且将用户信息简单的入库了。
二、新浪微博的第三方认证
我们也是首先要创建应用 http://open.weibo.com/
,打开http://open.weibo.com/widget/ 这个里边有各种应用微博的组件
本文出自 “fiting” 博客,请务必保留此出处http://zrwx123.blog.51cto.com/10193196/1651083
标签:第三方
原文地址:http://zrwx123.blog.51cto.com/10193196/1651083