Introduction to Sina Weibo API development-PHP Basics-user authorization

Source: Internet
Author: User
Tags oauth php basics
Now there are more and more people playing Weibo, and there are more and more third-party application development on weibo. by accident, I began to contact Sina Weibo API development. There are many resources for Sina Weibo API development, sina Weibo provides a developer platform, Web site is: http://open.weibo.com, it has now play microblogging more and more people, and third-party application development on Weibo is also growing, 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


$ 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.


$ _ 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


$ 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:



// 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.