Sina Weibo API Development profile PHP basic article-User authorization

Source: Internet
Author: User
Tags json oauth


Now playing micro Bo more and more people, and on the microblogging of Third-party application development is more and more, oneself in the occasional contact with Sina Weibo API development, Sina micro-blog API development Resources More, Sina Weibo provides a developer platform, URL is: http:// Open.weibo.com, it has a comprehensive Sina Weibo development of information, including the use and introduction of developers, various languages API function introduction document, SDK and many other materials.



In the process of development and learning, although not too difficult to feel, but there are still some problems we need to pay attention to, today in my development and learning process, the use of PHP for Sina micro-blog API development content to do a collation and description,



Pre-development of Sina micro-blogging API



First to the Sina Weibo open platform to download the SDK development package based on PHP, download the address is: http://code.google.com/p/libweibo/downloads/detail?name= Weibo-oauth-class-with-image-avatar-06-29.zip



After the download is completed and put into its own development environment and decompression, in which also contains demo demo program, we can refer to its sample program to write.



Sina Weibo API development of the most important user authorization process



In fact, many of the problems in the development process is focused on the user authorization phase, I developed a third-party application, using the OAuth authorization, on the OAuth licensing process in the Sina Weibo open platform has a very clear and complete introduction, we can go to http://open.weibo.com/ Wiki/oauth to see, I here from the point of view of the example development to introduce and explain.



  1. First obtain an unauthorized request Token





$o = new Weibooauth (Wb_akey, Wb_skey);
$keys = $o->getrequesttoken ();
Echo ($keys [' Oauth_token ']. ': '. $keys [' Oauth_token_secret ']);





We need to register an account in the Sina Weibo open platform, or use our Sina Weibo account to login directly, enter my application, and then follow the prompts to create our own third-party applications, and then we can get two licensed app key and app secret values, These two values are the key to our development and application.



Once the authorization value is obtained, we can use the above code to obtain the token request value, which will be saved in the $key array variable.



2. Then request user Authorization Token





$_session[' keys '] = $keys;
$aurl = $o->getauthorizeurl ($keys [' Oauth_token '], false, ' http://localhost/callback.php ');





Get unauthorized Request token value, we will use the above code can start to go to Sina Weibo authorization page authorization, $aurl is the authorization link page, we get $aurl can use header () directly jump to the authorization page, Then the user input Sina Weibo account number and password for authorization, after the authorization is completed, automatically jump back to you in the last parameter set in the callback page: http://localhost/callback.php, the link you can set to the previous page, so the authorization is completed automatically and then jump back.



It is important to note that the value of the keys that set the session is required, and it is needed in the following access to the authorized access token. A lot of friends may refer to its open platform above the instructions to authorize, can be found always error, is generally the problem, you have not set the keys of the session, in the following of course, can not get access to the value of token, this must be remembered.



  3. Access Token that are finally authorized by the user





$o = new Weibooauth (Wb_akey,
Wb_skey,
$_session[' Keys ' [' Oauth_token '],
$_session[' Keys '] [' oauth_token_secret ']);

$last _key = $o->getaccesstoken ($_request[' oauth_verifier '));
Echo ($last _key[' Oauth_token ']);





The code above finally gets the user-authorized access Token, a total of two values, which are stored in the $last_key array variable, and we can see that the next two parameters are the session values we set earlier. This is basically done, this is the Sina Weibo user authorization of a complete process.



Work after the authorization is completed



After the authorization is complete, we can begin to invoke the various API function interfaces provided by Sina Weibo for practical application development, here I will get the latest micro-blog record This interface for a simple explanation, others are similar.



To obtain the latest Sina Weibo information API interface function is: Public_timeline (), sample code look at the following:








//Get the top 20 newly updated public microblogging messages
$c = NE W 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);





Typically, after we get the user-authorized access token value, we store them in our user table and correspond to the accounts in our application, and then we don't have to authenticate every time we call Sina Weibo's API interface.



The above code is very simple, instantiate the Weiboclient object, and then call the interface function directly public_timeline can returned, if there is no error. Typically, the data format returned by the Sina Weibo API interface is in JSON format or XML format, and we are here to develop using PHP, the JSON-formatted data is inherently advantageous, and if you return JSON format data, use PHP functions directly json_decode () can be converted to PHP's commonly used array format.



Source: Red Grass Blog



Original address: http://www.hongxincao.com/archives/579.html


Related Article

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.