当前位置: 移动技术网 > IT编程>开发语言>PHP > php新浪微博登录接口用法实例

php新浪微博登录接口用法实例

2018年07月08日  | 移动技术网IT编程  | 我要评论

贯通的意思,app设计,文字图片大全

本文实例讲述了php新浪微博登录接口用法。分享给大家供大家参考。具体分析如下:

在做微博登陆之前是需要申请到app key 和app secret,这个的申请方式请去 open.weibo.com 申请相关内容.

在官网也有相关的开发文档http://open.weibo.com/wiki/可以查看相关资料,我这里下载的php的sdk直接进行的web网站应用.

下载sdk,配置好config文件,代码如下:

复制代码 代码如下:
<?php
header('content-type: text/html; charset=utf-8');
define( "wb_akey" , 'xxxxxxxxxx' );
define( "wb_skey" , 'xxxxxxxxxxxxxxxxxxxxxxxxx' );
define( "wb_callback_url" , 'http://xxxxxxxxxxxx/callback.php' );//回调地址
/*这里的回调地址是指如果用户同意授权,页面跳转至 your_registered_redirect_uri/?code=code     //your_registered_redirect_uri 就是你的回调地址。 */
//那就第一步需要首先引导用户进行授权。

include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );
$o = new saetoauth( wb_akey , wb_skey );
$code_url = $o->getauthorizeurl( canvas_page );
echo "<a href=$code_url>授权</a>";
//授权地址为:
https://api.weibo.com/oauth2/authorize?client_id=your_client_id&response_type=code&redirect_uri=your_registered_redirect_uri</pre>
?>


如果用户同意授权之后,在你的回调地址里需要获取 换取access token 来调用接口,获取信息,代码如下:
复制代码 代码如下:
if($_request['code']){
echo "sds";
$keys = array();
$keys['code'] = $_request['code'];
$keys['redirect_uri'] = canvas_page;
$tt= new saetoauth( wb_akey , wb_skey );
$bb = $tt->getaccesstoken('code',$keys);
var_dump($bb);
}

在成功获取到accesstoken之后,可以调用saetv2.ex.class.php的一切封装好的函数进行操作,例如,我这里做登陆功能就需要获取用户的信息,代码如下:
复制代码 代码如下:
/**
* 根据用户uid或昵称获取用户资料
*
* 按用户uid或昵称返回用户资料,同时也将返回用户的最新发布的微博。
* <br />对应api:users/show
*
* @access public
* @param mixed $uid_or_name 用户uid或微博昵称。
* @return array
*/
function show_user( $uid_or_name )
{
return $this->request_with_uid( 'https://api.t.sina.com.cn/users/show.json' , $uid_or_name );
}

希望本文所述对大家的php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网