首页 > php开发 > php新浪微博登录接口实例代码

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 就是你的回调地址。

那就第一步需要首先引导用户进行授权。

<?php
include_once ('config.php');
include_once ('saetv2.ex.class.php');
$o = new SaeTOAuth(WB_AKEY, WB_SKEY);
$code_url = $o->getAuthorizeURL(http: //pic3.phprm.com/2014/05/09/CANVAS_PAGE.jpg );
    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 来调用接口。获取信息,等等。。。

<?php
    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的一切封装好的函数进行操作,例如,我这里做登陆功能就需要获取用户的信息:

<?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);
    }
?>


本文地址:http://www.phprm.com/develop/61297.html

转载随意,但请附上文章地址:-)

标签:include request

相关文章

发表留言