php新浪微博登陆接口

在做微博登陆之前是需要申请到APP KEY 和App Secret ,这个的申请方式请去 open.weibo.com 申请相关内容。
在官网也有相关的开发文档//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" , '//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 );
}

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

关闭菜单