Sina Weibo API Development Introduction user Authorization (PHP Foundation) _php Tutorial

Source: Internet
Author: User
There are more and more people playing micro-blog, and the third-party application of micro-blog development is more and more, they began to contact the Sina Weibo API development, Sina Weibo API development of more resources, Sina Weibo provides a developer's platform, the URL is:/http, it has a very comprehensive Sina Weibo development information, including the use and introduction of developers, various languages API function introduction document, SDK and other materials.

Oneself in the process of development and learning, although the feeling is not too difficult, but there are still some problems need our attention, today in my development and learning process, simple to use PHP Sina Weibo API development of the content of a collation and description,

Pre-development for Sina Weibo API

First, to the Sina Weibo open platform download PHP-based SDK development package, the download address is:

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

The most important user authorization process for Sina Weibo API development

In fact, in the development process a lot of problems are focused on the user authorization at this stage, I developed a third-party application, using OAuth authorization, about the OAuth authorization process in the Sina Weibo open platform has a very clear and complete introduction, we can go to Wiki/oauth to see, I am here from the perspective of the development of the case to introduce and explain.

1. First obtain an unauthorized request Token
Copy CodeThe code is as follows:
$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 log in directly with our Sina Weibo account, enter my app and follow the prompts to create a third-party app that belongs to us, and we can get two licensed app key and app secret values after the creation is complete. These two values are the key to our application development.
Given the authorization value, we can use the above code to obtain the unauthorized request token value, which is stored in the $key array variable.
2. Then request the user to authorize tokens
Copy CodeThe code is as follows:
$_session[' keys '] = $keys;
Aurl = $o->getauthorizeurl ($keys [' Oauth_token '], false, ' http://localhost/callback.php ');

After obtaining the unauthorized request token value, we can use the above code to start preparing for authorization on the Sina Weibo licensing page, $aurl is the authorized link page, we can use the header () to jump directly to the authorization page after we get $aurl. Then the user enters the Sina Weibo account number and the password authorizes, after the authorization completes, automatically jumps back the callback page which you set in the last parameter: http://localhost/callback.php, the link you can set as the previous page, so the authorization completes automatically and then jumps back.
It is important to note that the value of keys is required to set the session, and it is required to obtain the authorization access token below. A lot of friends may refer to their open platform above the instructions to authorize, can find always error, is generally the problem, you did not set the session keys, in the following of course can not take access token value, this must be remembered.
3. Last access Token granted by the user
Copy CodeThe code is as follows:
$o = new Weibooauth (Wb_akey,
$_session[' Keys ' [' Oauth_token '],
$_session[' Keys ' [' Oauth_token_secret ']);
$last _key = $o->getaccesstoken ($_request[' oauth_verifier ');
Echo ($last _key[' Oauth_token ');

The above code eventually obtained a user-authorized access Token, a total of two values, they are stored in the $last_key array variable, we can also see that the following two parameters is the previous set of the session value. This is basically done, this is the Sina Weibo user authorization of a complete process.
Work after authorization is completed
After the authorization is completed, we can start to invoke the various API function interfaces provided by Sina Weibo for practical application development, here I get the latest Weibo record this interface to make a simple explanation, the others are similar.
The API interface functions for getting the latest Sina Weibo information are: Public_timeline (), sample code see below:
Copy CodeThe code is as follows:
Get the Top 20 latest updates for public Weibo messages
$c = new Weiboclient (Wb_akey,
$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);

Usually after we get the user-authorized access token value, we save them in our user table and correspond to the accounts in our app, then we don't have to authenticate every time we call the API interface of Sina Weibo.
The above code is very simple, instantiate the Weiboclient object, and then call the interface function directly public_timeline can get the information returned, if there is no error. Usually the Sina Weibo API interface return data format is generally JSON format or XML format, and we are developed in PHP, the use of JSON-formatted data has a congenital advantage, if the return of JSON format data, directly using the PHP function Json_decode () It can be converted to the array format commonly used by PHP.
Source: Hearts Blog
Original address: true techarticle Now there are more and more people playing micro-blog, and the third-party application of micro-blog development is more and more, their own in the occasional beginning to contact the Sina Weibo API development, Sina Weibo API development resources ...

  • Related Article

    Beyond APAC's No.1 Cloud

    19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

    Learn more >

    Apsara Conference 2019

    The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

    Learn more >

    Alibaba Cloud Free Trial

    Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

    Learn more >

    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: and provide relevant evidence. A staff member will contact you within 5 working days.