Home > php教程 > php手册 > body text

php新浪微博登录接口实例代码

WBOY
Release: 2016-05-25 16:46:37
Original
1197 people have browsed it

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

在官网也有相关的开发文档http://open.weibo.com/wiki/可以查看相关资料。

我这里下载的php的SDK直接进行的web网站应用。

下载SDK,配置好config文件。

<?php
header(&#39;Content-Type: text/html; charset=UTF-8&#39;);
define("WB_AKEY", &#39;xxxxxxxxxx&#39;);
define("WB_SKEY", &#39;xxxxxxxxxxxxxxxxxxxxxxxxx&#39;);
define("WB_CALLBACK_URL", &#39;http://xxxxxxxxxxxx/callback.php&#39;); //回调地址
?>
Copy after login

*这里的回调地址是指如果用户同意授权,页面跳转至 YOUR_REGISTERED_REDIRECT_URI/?code=CODE //YOUR_REGISTERED_REDIRECT_URI 就是你的回调地址。

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

<?php
include_once (&#39;config.php&#39;);
include_once (&#39;saetv2.ex.class.php&#39;);
$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?
Copy after login

client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI

如果用户同意授权之后,在你的回调地址里需要获取 换取Access Token 来调用接口。获取信息,等等。。。

<?php
    if ($_REQUEST[&#39;code&#39;]) {
        echo "sds";
        $keys = array();
        $keys[&#39;code&#39;] = $_REQUEST[&#39;code&#39;];
        $keys[&#39;redirect_uri&#39;] = CANVAS_PAGE;
        $tt = new SaeTOAuth(WB_AKEY, WB_SKEY);
        $bb = $tt->getAccessToken(&#39;code&#39;, $keys);
        var_dump($bb);
    }
?>
Copy after login

在成功获取到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(&#39;https://api.t.sina.com.cn/users/show.json&#39;, $uid_or_name);
    }
?>
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!