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, which has a very comprehensive Sina Weibo development of information, including the use and introduction of developers, the various languages of the API function documentation, SDK 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.