Introduction to Sina Weibo API development-user authorization (PHP basics) _ php instances

Source: Internet
Author: User
Tags oauth php basics
Although I feel that the development and learning process is not very difficult, we still need to pay attention to some problems. Today I am in the development and learning process, simply sort out and describe the content of Sina Weibo API development using PHP, and more people are playing Weibo now, and there are more and more third-party applications on Weibo, I accidentally began to contact the Sina Weibo API development, Sina Weibo API development resources are more, Sina Weibo provides a developer platform, web site is: http://open.weibo.com, it contains comprehensive Sina Weibo development materials, including developer usage and introduction, API function introduction documents in various languages, sdks, and other materials.

Although I feel that the development and learning process is not very difficult, we still need to pay attention to some problems. Today I am in the development and learning process, this article provides a brief description of the content of Sina Weibo API development using PHP,

Preparations before Sina Weibo API Development

First go to Sina Weibo Open Platform download PHP-based SDK Development Kit, is: http://code.google.com/p/libweibo/downloads/detail? Name‑weibo-oauth-class-with-image-avatar-06-29.zip

After the download is complete, put it in your development environment and decompress it. The demo program is also included in it. We can refer to the sample program for compiling.

The most important user authorization process for Sina Weibo API Development

In fact, many problems in the development process are concentrated at the user authorization stage. Third-party applications I developed use OAuth authorization, the OAuth authorization process is clearly and completely described on the Sina Weibo open platform. We can go to http://open.weibo.com/wiki/oauthto renew the authorization process. Here I will introduce and explain how to develop the authorization process from the perspective of authorization.

1. first obtain the unauthorized Request Token

The Code is as follows:


$ O = new WeiboOAuth (WB_AKEY, WB_SKEY );
$ Keys = $ o-> getRequestToken ();
// Echo ($ keys ['oss _ token']. ':'. $ keys ['oss _ token_secret ']);


We need to register an account on the Sina Weibo open platform, or directly use our Sina Weibo account to log on to my application, and then follow the prompts to create our own third-party application, after the creation, we can get two authorized App keys and App Secret values. These two values are the Key to our application development.
After obtaining the authorization value, we can use the above Code to obtain the unauthorized Request Token value, which will be saved in the $ key array variable.
2. Then, request the user to authorize the Token.

The Code is as follows:


$ _ SESSION ['keys '] = $ keys;
Aurl = $ o-> getAuthorizeURL ($ keys ['auth _ token'], false, 'HTTP: // localhost/callback. php ');


After obtaining an unauthorized Request Token value, we can use the above Code to start preparing to go to the Sina Weibo authorization page for authorization. $ aurl is the authorization link page, after getting $ aurl, we can use header () to directly jump to the authorization page. Then, the user enters the Sina Weibo account and password for authorization. After authorization, automatically jumps back to the callback page you set in the last parameter: http: // localhost/callback. php. You can set the link to the previous page. After authorization is completed, the link will automatically jump back.
It must be noted that the keys value of the session is required. It is required to obtain the authorized Access Token below. Many friends may refer to the instructions on the open platform to perform authorization, but they may find that there are always errors. This is generally the problem. You have not set the session keys value, of course, the value of Access Token cannot be obtained below. Remember this.
3. Finally, get the user-authorized Access Token

The Code is as follows:


$ O = new WeiboOAuth (WB_AKEY,
WB_SKEY,
$ _ SESSION ['keys '] ['oss _ token'],
$ _ SESSION ['keys '] ['oss _ token_secret']);
$ Last_key = $ o-> getAccessToken ($ _ REQUEST ['oss _ verifier ']);
Echo ($ last_key ['oss _ token']);


The above Code finally obtained the user-authorized Access Token. There are two values in total, which are saved in the $ last_key array variable. We can also see that, the following two parameters are the previously set session values. This is basically done. This is a complete process of Sina Weibo user authorization.
Work after authorization is completed
After the authorization is complete, we can start to call various API function interfaces provided by Sina Weibo for actual application development. Here I will give a brief description of this interface, others are similar.
The API function for getting the latest Sina Weibo information is public_timeline (). The sample code is as follows:

The Code is as follows:


// Obtain the first 20 latest public microblog messages
$ 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 );


Generally, after obtaining the user-authorized Access Token value, we store them in our user table and correspond to the account in our application, then we don't have to authenticate each api call on Sina Weibo.
The above code is very simple. instantiate the WeiboClient object, and then directly call the interface function public_timeline to get the returned information, if there is no error. Generally, the data returned by Sina Weibo api interfaces is in Json or xml format. However, if we use php for development, the data in Json format has inherent advantages, if Json format data is returned, you can directly use the php function json_decode () to convert it to the commonly used array format in php.
Source: hongxincao blog
Address: http://www.hongxincao.com/archives/579.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.