技术可能还是不到家,阅读了好久API文档都没有找到一个解决办法,最后阅读一篇文章的时候才恍然领悟原来Token可以重复用的我了去,原来一直就是卡在这里,把这里忽略掉了…我把别人的代码复制过来希望对大家有点帮助吧,我提醒自己以后别忘记了.
接入页面
call.php
include_once( 'sina_config.php' );
include_once( 'saetv2.ex.class.php' );
//获取到授权的url
$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
$code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );
//post或get方式调用该url,取得授权;授权完成后,新浪会调用我们这边传过去的回调地址:WB_CALLBACK_URL
request()->redirect($code_url);
回调地址页面(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) {
//取到授权后的api调用密钥,可用存起来,在有效期内多次调用api接口就不用再授权了
$_SESSION['token'] = $token;
$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
$ret = $c->update( $weiboStr ); //发送微博
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.");
}
}
博客的摘要提取
微博的字数为140字;其中汉字为1个字;我们使用计数函数得有所选择;对一个汉字,strlen()算为3个字符,而多字节统计函数mb_strlen()算1个字,符合我们的使用要求;
最后在发的微博中需要清除html标记和& nbsp;等
//获取当前微博内容(140字)
public function getWeibo()
{
$titleLen = mb_strlen($this->title, 'UTF-8');
//140字除去链接的20个字和省略符;剩115字左右,需要说明的是链接:无论文章的链接多长,在微博里都会被替换成短链接,按短链接的长度来计算字数;
$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;
}
//完全的去除html标记
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;
}