Php uses Sina Weibo API to develop user authorization function

Source: Internet
Author: User
Tags oauth
Php instance of Sina Weibo API. Sina Weibo has many resources for API development. Sina Weibo provides a developer platform on open.weibo.com, which contains comprehensive Sina Weibo development materials, including the usage and introduction of developers, API function introduction documents in various languages, sdks, and other materials. First, open it on Sina Weibo

Php instance of Sina Weibo API. Sina Weibo API development resources are more, Sina Weibo provides a developer platform, web site is: http://open.weibo.com, it has a very comprehensive Sina Weibo development information, including the usage and introduction of developers, API function introduction documents in various languages, sdks, and other materials. First, open it on Sina Weibo

Php instance of Sina Weibo API.

Sina Weibo API development resources are more, Sina Weibo provides a developer platform, web site is: http://open.weibo.com, it has a very comprehensive Sina Weibo development information, including the usage and introduction of developers, API function introduction documents in various languages, sdks, and other materials.

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

Put the downloaded package in the development environment and decompress it. It also contains the demo program. You can refer to the sample program for compiling.

The most important user authorization process for Sina Weibo API Development

The developed third-party application uses OAuth authorization. The OAuth authorization process is clearly and completely described on the Sina Weibo open platform. You can go to http://open.weibo.com/wiki/oauthto authorize.

1. First, obtain the unauthorized Request Token.

Sample Code:

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

You need to register an account on the Sina Weibo open platform, or log on directly using the Sina Weibo account to enter the application, and then follow the prompts to create your own third-party application, after creation, you can obtain two authorized App keys and App Secret values. These two values are the Key to application development.
After obtaining the authorization value, you can use the above Code to obtain the unauthorized Request Token value, which will be saved in the $ key array variable.

2. Request the user to authorize the Token

Sample Code:

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

After obtaining the value of the unauthorized Request Token, you 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, you 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.

Note: It is required to set the keys value of the session. It is used 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. Get the user-authorized Access Token

Sample Code:

$ 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 obtains the user-authorized Access Token, which has two values in total. They are saved in the $ last_key array variable. You 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 authorization is complete, you can start to call various API function interfaces provided by Sina Weibo for actual application development. Here, we will briefly describe the interface for obtaining the latest Weibo record, others are similar.
The API function for getting the latest Sina Weibo information is public_timeline (). The sample code is as follows:

Sample Code:

// 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 values, they are stored in the User table, corresponding to the account in the application, then, you do not need to authenticate each api call on Sina Weibo.

Instantiate the WeiboClient object, and then directly call the interface function public_timeline to obtain 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 php is used for development, Json data 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.

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.