Home  >  Article  >  Backend Development  >  php新浪微博登录接口用法实例_php技巧

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

WBOY
WBOYOriginal
2016-05-16 20:27:371185browse

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

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

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

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

复制代码 代码如下:
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 "授权";
//授权地址为:
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
?>


如果用户同意授权之后,在你的回调地址里需要获取 换取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或昵称返回用户资料,同时也将返回用户的最新发布的微博。
*
对应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程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn