新浪微博API开发简介之用户授权(PHP基础篇)_php实例
现在玩微博的人越来越多了,而关于微博的第三方应用开发也越来越多,自己在偶然间开始接触了新浪微博API开发,新浪微博API开发的资源比较多,新浪微博提供了一个开发者的平台,网址是:http://open.weibo.com,它里面有很全面的新浪微博开发的资料,包括开发者的使用和介绍,各种语言的API函数介绍文档,SDK等多种资料。
自己在开发和学习的过程中,感觉虽然没有太大难度,但还是有一些问题是需要我们注意的,今天就我在开发和学习的过程中,简单的对利用PHP进行新浪微博API开发的内容进行一个整理和说明,
新浪微博API开发前的准备工作
首先到新浪微博开放平台下载基于PHP的SDK开发包,下载地址是:http://code.google.com/p/libweibo/downloads/detail?name=weibo-oauth-class-with-image-avatar-06-29.zip
下载完成后放到自己的开发环境中并解压,在其中也包含了demo演示程序,我们可以参考其样例程序进行编写。
新浪微博API开发最重要的用户授权过程
其实在开发过程中很多的问题都是集中在用户授权这个阶段,我开发的第三方应用,使用的是OAuth授权,关于OAuth授权的流程在新浪微博开放平台里有很清晰完整的介绍,我们可以到http://open.weibo.com/wiki/Oauth去查看,我这里从实例开发的角度进行介绍和说明。
1.首先获取未授权的Request Token
$o = new WeiboOAuth( WB_AKEY , WB_SKEY );
$keys = $o->getRequestToken();
//echo($keys['oauth_token'].' : '.$keys['oauth_token_secret']);
我们需要在新浪微博开放平台中注册一个帐号,或直接使用我们的新浪微博帐号登录,进入我的应用,然后按照提示创建属于我们自己的第三方应用,创建完成之后我们可以得到两个授权的App Key和App Secret值,这两个值就是我们开发应用的关键。
得到授权值后,我们就可以利用上面的代码获得未授权的Request Token值了,它们会保存在$key数组变量中。
2.然后请求用户授权Token
$_SESSION['keys'] = $keys;
aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false , 'http://localhost/callback.php');
得到未授权的Request Token值后,我们就利用上面的代码可以开始准备去新浪微博授权页面进行授权,$aurl就是授权链接页面,我们得到$aurl后就可以利用header()直接跳转到该授权页面,然后用户输入新浪微博帐号和密码进行授权,授权完成后,自动跳回你在最后一个参数里面设置的回调页面:http://localhost/callback.php,该链接你可以设置为上一个页面,这样授权完成之后就会自动又跳转回去了。
需要注意的是设置session的keys的值是必须的,它在下面获取到授权的Access Token中是需要用到的。很多的朋友可能会参考其开放平台上面的说明来进行授权时,可发现总是出错,一般都是这个问题,你并未设置session的keys值,在下面当然取不到Access Token的值了,这个一定要记住了。
3.最后得到用户授权的Access Token
$o = new WeiboOAuth( WB_AKEY ,
WB_SKEY ,
$_SESSION['keys']['oauth_token'] ,
$_SESSION['keys']['oauth_token_secret'] );
$last_key = $o->getAccessToken( $_REQUEST['oauth_verifier'] ) ;
echo($last_key['oauth_token']);
上面的代码就最终获得了用户授权的Access Token,共两个值,它们保存在$last_key数组变量里面,我们也可以看到,后面的两个参数就是前面我们设置的session值。到此就基本完成了,这就是新浪微博用户授权的一个完整的过程。
授权完成后的工作
在授权完成之后,我们就可以开始调用新浪微博提供的各类API函数接口进行实际应用的开发了,在这里我就获取最新微博记录这个接口进行一个简单说明,其他都类似。
获取最新新浪微博信息的API接口函数是:public_timeline(),样例代码看下面:
//获取前20条最新更新的公共微博消息
$c = new WeiboClient( WB_AKEY ,
WB_SKEY ,
$oauth_token ,
$oauth_token_secret );
$msg = $c->public_timeline();
if ($msg === false || $msg === null){
echo "Error occured";
return false;
}
if (isset($msg['error_code']) && isset($msg['error'])){
echo ('Error_code: '.$msg['error_code'].'; Error: '.$msg['error'] );
return false;
}
print_r($msg);
通常我们在得到用户授权的Access Token值之后,就把它们保存在我们的用户表中,与我们的应用中的帐号进行对应,之后我们在调用新浪微博各api接口时就不用每次都去认证了。
上面的代码很简单,实例化WeiboClient对象,然后直接调用接口函数public_timeline就可以得到返回的信息,如果没有错误的话。通常新浪微博api接口返回的数据格式一般为Json格式或xml格式,而我们在此是用php进行开发,则使用Json格式的数据就有先天的优势,如果返回Json格式数据的话,直接使用php函数json_decode()就可以转换为php常用的array数组格式了。
来源:红心草博客
原文地址:http://www.hongxincao.com/archives/579.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

As web applications continue to develop and change, handling parallel and asynchronous requests has become an important topic in PHP backend API development. In traditional PHP applications, requests are performed synchronously, that is, a request will wait until a response is received, which will affect the response speed and performance of the application. However, PHP now has the ability to handle parallel and asynchronous requests. These features allow us to better handle a large number of concurrent requests and improve the response speed and performance of the application. This article will discuss how to deal with PHP backend API development

With the continuous development of Java technology, Java API has become one of the mainstream solutions developed by many enterprises. During the development process of Java API, a large number of requests and data often need to be processed, but the traditional synchronous processing method cannot meet the needs of high concurrency and high throughput. Therefore, asynchronous processing has become one of the important solutions in JavaAPI development. This article will introduce asynchronous processing solutions commonly used in Java API development and how to use them. 1. Java differences

How to create a group chat on Sina Weibo app? Sina Weibo app is a software that allows users to socialize on this software. Users can follow each other here and send private messages to other users here. In other words, users can socialize on this software. Chat. In addition, this software also allows users to create group chats and then chat. Many users still don’t know how to create a group chat. The editor below has compiled methods for creating group chats for your reference. How to create a group chat on Sina Weibo app 1. First, open the software, find and click the "Gear" option in the upper right corner of the "Message" interface; 2. Then, click on the pop-up option box

With the rapid development of the Internet, more and more applications adopt the Web architecture, and PHP, as a scripting language widely used in Web development, has also received increasing attention and application. With the continuous development and expansion of business, the performance problems of PHPWeb applications have gradually been exposed. How to perform performance tuning has become an important challenge that PHPWeb developers have to face. Next, this article will introduce performance tuning techniques in PHP back-end API development to help PHP developers better

With the rapid development of the Internet, more and more business requirements require data exchange through the network, in which API plays a vital role as an intermediary for data transmission. In PHP back-end API development, scalability design is a very important aspect, which can make the system more adaptable and scalable, and improve the robustness and stability of the system. 1. What is scalability design? Scalability means that a certain function of the system can add additional functions to meet business needs without affecting the original performance of the system. exist

The best cross-domain strategy and implementation in PHP API development With the rise of RESTful API, PHP, as a back-end development language, has also been widely used in many web applications. When developing RESTful API, cross-domain access issues often need to be considered. This article will discuss the best cross-domain strategies in PHP API development and how to implement them. Cross-domain (Cross-OriginResourceSharing) refers to a document or script in one domain trying to request another

In PHPAPI development, input and output verification is a very important and essential step. Properly understanding and strictly implementing input and output validation can help us reduce many potential problems, prevent security vulnerabilities and errors, and also improve the quality and reliability of applications. The following are the best input and output validation practices in PHP API development. Input Validation When developing APIs, we should always follow the principle of "good input can produce good output". Therefore, input validation is a very important part. The following is

1. Log in to Weibo, view personal information, and click on the small crown icon, as shown in the picture. 2. Click to activate membership. 3. Select your activation time and payment method and click Confirm to activate. After reading the above related methods on how to activate Sina Weibo membership, you should be able to do it yourself.
