Home  >  Article  >  Backend Development  >  PHP implements synchronous sending of Weibo_PHP tutorial

PHP implements synchronous sending of Weibo_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:10787browse

Introduction: When designing a blog site, sometimes it is necessary to publish an article on Weibo simultaneously. This article explains the basic way to implement this functionality.



Preparation ​ As a developer of Sina Weibo, identity verification is required; The review of personal identity authentication usually takes one working day; The next step is to submit the website for review, and for domestic applications, submit the registration number. If you are overseas, just submit the overseas certificate from the website where you are located; it also takes about one working day; After passing the personal identity review, you can create applications and call interfaces, and the permissions obtained at this time are relatively low; If the website is not submitted for review or fails to pass the review, it will have no impact on posting on Weibo; only "Unreviewed Application" will be displayed under the posted Weibo; After review, the name of the website application is displayed: image ​

Call interface ​ The Weibo open platform provides testing tools; Before developing access, you must first ensure that the test Weibo can be sent out through this testing tool; http://open.weibo.com/tools/console?uri=statuses/update&httpmethod=POST&key1=status&value1=%E5%BE%AE%E5%8D%9A%E6%9D%A5%E8%87%AAAPI%E6 %B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7 ​ The API reference document for posting on Weibo is: http://open.weibo.com/wiki/2/statuses/update ​ All interfaces for posting Weibo require permission authentication; after passing the authentication, you will get an access_token (access key); the validity period of the key varies according to the user level; Those who fail the web review will be given 1 day; ordinary users who pass the review will be given 7 days; During the validity period, there is no need to interact with the Sina server for permission authentication. As long as the token is saved locally, it can be used to call various Weibo APIs (read, write, obtain audience information, etc.) ​

Permission authentication ​ There are three ways to authenticate permissions: ​ By username and password; This is the easiest to understand. Write the username and password of the Weibo account in the program and authenticate through API calls; but it should be noted that this interface is provided for developing apps and cannot be used by web applications; ​ Through web callback; Need to interact with sina server and provide callback address; get access_token from the callback address; ​ The third method is the code method, which has not been studied carefully and is skipped; The web application only supports the second authorization method; the following details the use of the second method: ​ Download the SDK provided by Sina, which contains demo and API packaging classes; http://open.weibo.com/wiki/SDK ​

Access page
​ ==call.php========================
include_once( 'sina_config.php' ); include_once( 'saetv2.ex.class.php' ); ​ //Get the authorized url $o = new SaeTOAuthV2( WB_AKEY , WB_SKEY ); $code_url = $o->getAuthorizeURL( WB_CALLBACK_URL ); ​ //Call the url via post or get to obtain authorization; after the authorization is completed, Sina will call the callback address passed by us: WB_CALLBACK_URL request()->redirect($code_url);


Callback address page (WB_CALLBACK_URL):
===callback.php====================
$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY ); if (isset($_REQUEST['code'])) { $keys = array(); $keys['code'] = $_REQUEST['code']; $keys['redirect_uri'] = WB_CALLBACK_URL; try { $token = $o->getAccessToken( 'code', $keys ) ; } catch (OAuthException $e) { echo "weibo.com get access token err."; Log_err ("Weibo.com get access token err."); Return ; } } ​ if ($token) { //After obtaining the authorized api call key, you can save it. If you call the api interface multiple times within the validity period, you do not need to authorize it again. $_SESSION['token'] = $token; $c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] ); $ret = $c->update( $weiboStr ); //Send Weibo if ( isset($ret['error_code']) && $ret['error_code'] > 0 ) { ​ ​ ​ $str = "Weibo.com Send failed. err info:" . $ret['error_code'] . '/' . $ret['error']; LOG_ERR($str); } else {                                                                     LOG_INFO("Weibo.com Send Success."); } }

Blog summary extraction ​ The number of characters in Weibo is 140; Chinese characters are 1 character; we have to be selective in using the counting function; for a Chinese character, strlen() counts as 3 characters, while the multi-byte statistical function mb_strlen() counts as 1 character , meets our usage requirements; Finally, you need to clear the html tags and & nbsp; etc. in the Weibo post. ​
//Get the current Weibo content (140 words) Public function getWeibo() { $titleLen = mb_strlen($this->title, 'UTF-8'); //140 words excluding the 20 characters of the link and the ellipses; there are about 115 words left. What needs to be explained is the link: no matter how long the link of the article is, it will be replaced with a short link in Weibo, and the calculation is based on the length of the short link. word count;                    $summaryLen = 115 - $titleLen; ​ $pubPaper = cutstr_html($this->summary); If(mb_strlen($pubPaper, 'UTF-8') >= $summaryLen)                 $pubPaper = mb_substr($pubPaper,0,$summaryLen,'UTF-8'); ​ $pubPaper = sprintf('[%s]%s...%s', $this->title , $pubPaper , aurl('post/show', array('id' => $this-> id))); ​           return $pubPaper; } ​ //Completely remove html tags function cutstr_html($string) { $string = strip_tags($string); $string = preg_replace ('/n/is', '', $string); $string = preg_replace ('/ | /is', '', $string); $string = preg_replace ('/ /is', '', $string); ​ Return $string; }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735049.htmlTechArticleIntroduction: When designing a blog site, sometimes it is necessary to publish an article on Weibo simultaneously. This article explains the basic way to implement this functionality. Preparation work As a member of Sina Weibo...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn