Introduction to Sina Weibo API development-PHP basics-there are more and more people playing Weibo with user authorization, and more third-party application development 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
$ 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.