300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > php 新浪微博登陆 PHP使用新浪微博登入第三方网站实例代码

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

时间:2022-01-01 17:54:44

相关推荐

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

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

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

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

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

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

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

set_time_limit(0);

switch ($_GET['a'])

{

case 'login';

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

$url="/oauth2/authorize?client_id=xxxxxx&state=".$_SESSION['state']."&redirect_uri=/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='/oauth2/access_token';

$post_data =

array(

'client_id=xxxxxx',

'client_secret=xxxxxx',

'grant_type=authorization_code',

'code='.$code.'',

'redirect_uri=/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="/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('', $delay =0,$js = false,$jsWrapped = true, $return = false);

}

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

exit('time out or err');

break;

default:

}

/**

* @author 若愚动力

* @desc CURL 远程调用

* @date -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的参数和一些业务逻辑自己进行调整,有什么问题,欢迎留言

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。