php 新浪微博登陆,PHP使用新浪微博登入第三方网站实例代码

本文详细介绍了如何使用PHP结合新浪微博API实现OAuth2.0授权,完成第三方网站的一键登录功能。首先注册并完善微博开放平台资料,获取AppKey和AppSecret,然后通过API文档学习登录接口,编写PHP代码实现授权和回调处理,最终实现用户信息的获取与存入数据库。
摘要由CSDN通过智能技术生成

之前我写过一个使用php使用QQ一键登入第三方网站的教程,今天我再给大家分享PHP使用新浪微博API一键登入第三方的网站,好吧,不说废话,下面开始。注册登入新浪微博以后,可以点在新浪微博底部的开放平台链接进入,或者直接就使用链接http://open.weibo.com/authentication访问,

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

以上是整个实现登入的步骤效果,具体的步骤现在开始,先完善个人资料和认证身份,需要一点时间,下面是截图

0818b9ca8b590ca3270a3433284dd417.png

下面是添加新网站和获取App Key 和 App Secret

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

接下来在创建一个web网站应用,网址http://open.weibo.com/development,点击创建应用,选择在第三方网页内访问使用,接下来完成填写内容即可

到了这一步,就需要开始查看API知识了,API文档地址 http://open.weibo.com/wiki/index.php/API%E6%96%87%E6%A1%A3

咱们选择登录/OAuth 2.0接口进行认证和登入操作,各类参数参考这里http://open.weibo.com/wiki/OAuth2/access_token,我直接开始编码了

set_time_limit(0);

switch ($_GET['a'])

{

case 'login';

$_SESSION['state']=time();

$url="https://api.weibo.com/oauth2/authorize?client_id=xxxxxx&state=".$_SESSION['state']."&redirect_uri=http://www.yourdomain.com/sina_login.php?a=callback";

redirect($url, $delay =0,$js = false,$jsWrapped = true, $return = false);exit;

break;

case 'callback';

$code=daddslashes($_GET['code']);

$state=daddslashes($_GET['state']);

if(($code=='') or ($state<>$_SESSION['state']))

{

exit('err,please back');

}

$url='https://api.weibo.com/oauth2/access_token';

$post_data =

array(

'client_id=xxxxxx',

'client_secret=xxxxxx',

'grant_type=authorization_code',

'code='.$code.'',

'redirect_uri=http://www.yourdomain.com/sina_login.php?a=callback',

);

$backaccess_token=vita_get_url_content($url,$post_data,60);

$backaccess_token=json_decode($backaccess_token);

$access_token=$backaccess_token->access_token;

$uid=$backaccess_token->uid;

if ($access_token)

{

//此处根据你的db进行查询是否已经有这条数据

//db操作略,如果有取出用户userid和用户名username

if (empty($userid))

{

//返回用户信息

$url="https://api.weibo.com/2/users/show.json?access_token=".$access_token."&uid=".$uid."";

$userinfo=vita_get_url_content($url,'',60);

$userinfo=json_decode($userinfo);

//成功获取用户昵称,一起返回的还有很多,自己可以选择

$username=$userinfo->screen_name ;

//执行数据的插入(db操作略),然后再返回用户插入的userid

}

//进行赋值session

$_SESSION['userid']=$userid;

$_SESSION['username']=$username;

//成功并跳转

redirect('http://www.yourdoamin.com', $delay =0,$js = false,$jsWrapped = true, $return = false);

}

//此处为接口出现超时和错误等的处理

exit('time out or err');

break;

default:

}

/**

* @author 若愚动力

* @desc CURL 远程调用

* @date 2013-12-17

* @param $url

* @param $arraypost 数组

* @return 返回远程html

*/

function vita_get_url_content($url,$arraypost="",$timeout = 15)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

if (is_array($arraypost))

{

$arraypost= implode('&',$arraypost);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS,$arraypost);

}

$file_contents = curl_exec($ch); curl_close($ch); return $file_contents;

}

?>

整个实现过程就完成了,里面xxxxx的参数和一些业务逻辑自己进行调整,有什么问题,欢迎留言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值