Words2Weibo友好同步WORDPRESS博客日志和评论到新浪微博
|Words2Weibo插件能友好同步wordpress博客评论和日志到新浪微博,用的是新浪微博Basic Authorization认证,支持自定义修改微博来源显示,可以自己修改同步微博模板。(有垃圾评论过滤设置,不用担心把垃圾评论同步到自己的微博)
点击这里下载插件:words2weibo
这两天又修改了下,主要是对中英文混合文字的长度计算和文字截取更精确了,插件很小,注释很详细,自己完全可以参照进行修改,我直接贴出源代码在下面,插件有两个文件words2weibo.php和include/IsgdUrlShortener.class.php
words2weibo.php源码:
<?php /* Plugin Name: Words2Weibo Plugin URI: http://littlebar.tk?p=1565 Description: 实时友好同步WORDPRESS日志和评论到新浪微博。 Submit tweets to sina weibo in response to any new posts or comments on your blog. Version: 1.0 Author: Jungle Author URI: http://little8.com */ require_once(dirname(__FILE__) . '/include/IsgdUrlShortener.class.php'); /** 微博账号和API设置 */ function update_sina_t($status){ //修改为你自己的新浪微博账号 $username = "你的新浪微博账号"; //你的密码 $password = "微博密码"; $api_url = 'http://api.t.sina.com.cn/statuses/update.json'; //修改为你自己的AppKey,没有可以去http://open.t.sina.com.cn/申请一个,如果懒得去的话可以用我的3827708121,只不过微博来源会显示为Words2Weibo $body = array( 'status' => $status,'source'=>'你的新浪AppKey'); $headers = array( 'Authorization' => 'Basic '.base64_encode("$username:$password") ); $request = new WP_Http; $result = $request->request( $api_url , array( 'method' => 'POST', 'body' => $body, 'headers' => $headers ) ); } /** 当有评论发表时把评论同步到新浪微博 */ function comment_post_2_sina_t($commentId){ $sina_t = get_comment_meta($commentId, 'sina_t', true); if($sina_t) return; // 只同步经过审核APPROVED的评论 if (!$approvalStatus) { $approvalStatus = wp_get_comment_status($commentId); } if (($approvalStatus == 1) || ($approvalStatus == 'approve') || ($approvalStatus == 'approved')) { // 获得评论 $comment = get_comment($commentId); if (!$comment) { return; } // 由评论IP获得对应日志 $post = get_post($comment->comment_post_ID); //将评论所属日志标题转换为话题 $post_topic .= "#{$post->post_title}#"; $comment = get_comment($commentId); //在评论作者前加上@ $atcommentauthor .= "@{$comment->comment_author}"; //获得评论对应日志链接 $link = get_permalink($comment->comment_post_ID); // 用IS.GD缩短评论链接网址 if ($link) { $link .= "#comment-{$commentId}"; $shortener = new IsgdUrlShortener(); $link = $shortener->shortenUrl($link); } //省略号 $read_more_mark .= "..."; //构建微博样式 即 @评论作者>#评论对应日志标题#评论内容 $tweet = "{$atcommentauthor}>{$post_topic}{$comment->comment_content}"; // mb_strlen+strlen计算中英文混合字符长度,如果微博超过128字(518/2个字符,预留24个字符给链接,链接在后面得算24字符12字) if (strlen($tweet) + mb_strlen($tweet,'UTF8') > 512) { //则截取评论前90字(180字符) $comm_content = mb_strimwidth(strip_tags(trim($comment->comment_content)),0,180); //构建新的微博样式 $tweet = "{$atcommentauthor}>{$post_topic}{$comm_content}{$read_more_mark}"; } // 如果上边的微博还是超过128字, if (strlen($tweet) + mb_strlen($tweet,'UTF8') > 512) { //截取评论前232字符(116字) $comm_content = mb_strimwidth(strip_tags(trim($comment->comment_content)),0,232); //构建新的微博样式 (去掉了{$post_topic}#评论对应日志标题#) $tweet = "{$atcommentauthor}>{$comm_content}{$read_more_mark}"; } //给微博加上链接 $status = "{$tweet}{$link}"; //执行同步更新 update_sina_t($status); add_post_meta($commentId, 'sina_t', 'true', true); } } /** 当有日志被发表时,发一条微博到新浪微博 */ function publish_post_2_sina_t($postId){ $sina_t = get_post_meta($postId, 'sina_t', true); if($sina_t) return; $post = get_post($postId); $link = get_permalink($postId); // 用IS.GD缩短日志链接网址 if ($link) { $shortener = new IsgdUrlShortener(); $link = $shortener->shortenUrl($link); } //将日志标题转换为话题 $post_topic .= "#{$post->post_title}#"; //截取日志前80字 $post_content = mb_strimwidth(strip_tags(trim($_POST['post_content'])),0,80); //省略号 $read_more_mark .= "..."; //构建一个自定义前缀 $newposts .= "#自定义一个前缀不要用汉字比如我是Little8NewPost#"; // 日志微博样式 $tweet = "{$newposts}>{$post_topic}{$post_content}{$read_more_mark}{$link}"; $status = "{$tweet}"; //执行同步更新 update_sina_t($status); add_post_meta($post_ID, 'sina_t', 'true', true); } // 评论发表后立即同步 add_action('comment_post', 'comment_post_2_sina_t', 0); // 日志发表后立即同步 add_action('publish_post', 'publish_post_2_sina_t', 0); ?>
include/IsgdUrlShortener.class.php源码:
<?php /** Shorten a URL, using an external API. 缩短获得的网址,默认用IS.GD短网址服务,你可以自己更换apiURL,使用不同短网址服务。 This uses the IS.GD shortener by default. YOU can change the apiURL as you wish. */ class IsgdUrlShortener { var $apiUrl; function __construct() { $this->apiUrl = 'http://is.gd/api.php?longurl='; } /** Shorten a URL so that it'll show up nicely in a tweet. (And so that our tweet isn't too long).缩短网址,这样微博字数不会太多。 */ function shortenUrl($link) { $link = urlencode($link); $url = $this->apiUrl . $link; $c = curl_init($url); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $short = curl_exec($c); curl_close($c); return $short; } } ?>
注意上传前请把words2weibo.php中
//14行 $username = "你的新浪微博账号"; //15行 $password = "微博密码"; //17行 $body = array( 'status' => $status,'source'=>'你的新浪App Key'); //108行 $newposts .= "#自定义一个前缀不要用汉字比如我Little8NewPost#";
分别替换为你自己的相关信息 比如:
$username = "words2weibo@sina.com"; $password = "love3344520"; $body = array( 'status' => $status,'source'=>'3827708121'); $newposts .= "#Little8NewPost#";
如果你没有APP KEY 可以去新浪微博开放平台申请一个就能用了,但要通过审核之后才会在新浪微博里显示你的微博来源。
同步到新浪微博的效果如下:
//多余的话↓
默认同步日志到新浪微博模板:
自定义前缀+#日志标题#+日志前80字内容+省略号…+日志链接(为IS.GD短网址)
默认同步评论到新浪微博模板:
@评论作者+#评论对应日志标题#+评论内容+评论链接(为IS.GD短网址)
如果消息长度大于140字则只截取 评论内容 前90字(可自定义)
如果仍然超过140字则 去掉#评论对应日志标题# 并截取评论前116字
如果只想同步日志或者评论删除对应代码则可。
关于同步博客到微博的纠结了很久,各种同步方法几乎都尝试过,相关日志也写了好几篇,但无奈自己不懂PHP,基本上都是用他人做好的插件,但这些插件总是不能满足自己想要的效果,于是自己常常瞎折腾。抄来抄去,最近几天终于折腾出一个自己的插件能满足自己的需求。
aram int $visible 微博的可见性,0:所有人能看,1:仅自己可见。测试只能是0 …
V.GD链接接口问题已经解决
这个还是很实用的,如果能直接使用就好了。
@读卡器, 这是个插件 能直接使用的哈
The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin
插件什么地方出问题了 搞得我不能输出FEED了
去掉了require_once(dirname(__FILE__) . ‘/include/IsgdUrlShortener.class.php’);直接把缩短网址代码写进了WORDS2WEIBO 这样问题解决了。
V.GD
短链接换为了更短的V.GD
function __construct() {
$this->apiUrl = ‘http://v.gd/create.php?url=’;
}
错误修正了 应该设置返回格式为SIMPLE v.gd/create.php?format=simple&url=
只截取LINK后11个字符
@Jungle, $link = substr(($link),11); 只截取LINK后11个字符
substr(string,start,length)
$link = substr(($link),11);原来是表示从第11后开始截取字符
如$link = substr(“http://v.gd/6HbiZv”,11); 将返回/6HbiZv
修改错了不?怎么不见有显示啊
//去掉了链接的http://
$short = mb_strimwidth(($short),8,18);
唉不懂技术太费神了一个引号问题费了一天也没有解决,最后请教了QQ群CSDN-PHP里的大侠Mistruster才解决:
Mistruster 2010-12-18 23:16:05
‘UTF8‘错误
Mistruster 2010-12-18 23:17:02
mb_strlen($x,‘UTF8‘)
改为
mb_strlen($x,’UTF8′)
// if the tweet was too long, only leave 90 characters in comm-content
if ((strlen($tweet) + mb_strlen($tweet,‘UTF8‘)) / 2 > 256) {
$comm_content = mb_strimwidth(strip_tags(trim($comment->comment_content)),0,180);
$tweet = “{$atcommentauthor}>{$post_topic}{$comm_content}{$read_more_mark}”;
}
// if the tweet was still too long, leave comm_content_cut, atcommentauthor and link
if ((strlen($tweet) + mb_strlen($tweet,‘UTF8‘)) / 2 > 256) {
$comm_content = mb_strimwidth(strip_tags(trim($comment->comment_content)),0,232);
$tweet = “{$atcommentauthor}>{$comm_content}{$read_more_mark}”;
}
if ((strlen($tweet) + mb_strlen($tweet,‘UTF8‘)) / 2 > 256)好像除2是多余的直接大于512不就得了么?
if (strlen($tweet) + mb_strlen($tweet,‘UTF8‘) > 256)
这个算法用strlen、mb_strlen计算中英文混排字符串长度才是精确的。
改了计算字符长度函数mb_strlen为strlen,另外也把截取文字的函数由mb_substr改成了mb_strimwidth,这样如果所要生成的微博里既有汉字又有英文和数字的时候,微博长度计算会更精确一点。再加几个数字测试123
在strlen计算时,对待一个UTF8的中文字符是3个长度,英文字符是1个长度,mb_strimwidth截取是中文是两个长度,英文是一个长度。测试好像成功了哦,但是中文双引号计算长度不准确问题得怎么解决呢?测试“”“”
哇哈哈,好像不用担心,如上面这条评论同步到新浪微博有141个字了但竟能同步过去。这是怎么一回事呢?不清楚,难道是因为中文双引号的原因?不过很高兴我的微博可以超越新浪140字的限制了!我再多输几个“”“”“”
上面这条评论#comment-3642同步过去会有143字超过了3个字的,但竟然能同步成功,有网址有真相:t.sina.com.cn/1659950807/24ENbBJPB不知最多可以超过多少字呢?再来十个双引号“一”二“三”四“五”六”七“八”九“十
@威言威语, 肯定不是的哈
$username = “words2weibo@sina.com”;
$password = “love3344520”;
你的账号密码?哈哈
为什么链接要用IS.GD生成的短网址呢?因为通过api.t.sina.com.cn/statuses/update.json接口发消息到微博 提交的字数不能超过140汉字 或者280个字母 而提交的内容里如果包含网址 网址长度是按照字母个数来计算的而不是像通过新浪网页微博发送框里所有网址都是12个字来计算的。所以如果这个页面的网址太长了,那么同步的评论内容就会减少,甚至评论和日志不同步。例如本评论链接长度是124个字母即抵62汉字,评论内容早超过140字了,只能按照插件里因字数超过限制来重新生成同步内容 即使是最后一个算法(见下面)来算 $status长度=116+62+评论作者昵称所在字长+长度为2的省略号=180+了 远超过140字 则不能同步。
但变成短网址后长度就固定为12了,
按照第一个算法 $status长度=评论作者昵称长+标题长+90+省略号2+链接12 只要标题长度+评论作者昵称长不过36就能同步 (如果还是长了就会用这二个算法)
第二个算法 $status长度=116+12+评论作者昵称所在字长+省略号2 只要评论作者昵称不超过10字就能同步到微博(一般没有取这么长的昵称吧?取了只能怪他人品不好)
//第一个算法 if the tweet was too long, only leave 90 characters in comm-content
if (mb_strlen($tweet) > 130) {
$comm_content = mb_substr(strip_tags(trim($comment->comment_content)),0,90);
$tweet = “{$atcommentauthor}>{$post_topic}{$comm_content}{$read_more_mark}”;
}
//第二个算法 if the tweet was still too long, leave 116 characters of comm_content, atcommentauthor and link
if (mb_strlen($tweet) > 130) {
$comm_content = mb_substr(strip_tags(trim($comment->comment_content)),0,116);
$tweet = “{$atcommentauthor}>{$comm_content}{$read_more_mark}”;
}
$status = “{$tweet}{$link}”;
如果不生成短网址
但按照第一个算法得出 标题长度+评论作者昵称长度应该是33小于36的 也就是$status长度是137小于140为什么同步到新浪微博t.sina.com.cn/1659950807/l4Ctx6M7B却是按照第二个算法得出的结果呢?因为我用来计算长度的函数mb_strlen() 数英文字母是有一算一 不是把两个字母才算成一个 这样 标题长度+评论作者昵称长度=50大于36 所以就只能按第二个算法处理了。唉 说到了我的难处了,因为根本不懂PHP,只能东抄抄西瞟瞟,不会把英语字母符号等单独拿出来算……
mb_substr字符串为中英混合时数不清楚
mb_strimwidth如果字符串有汉字字母和数字时好像返回结果尾部容易出现乱码
终于Words2Weibo这个插件调试好了 虽然不懂技术 但爱折腾 终有收获:这是我的第一个WordPress插件哦。
关于同步博客到微博的纠结了很久,各种同步方法几乎都尝试过,相关日志也写了好几篇,但无奈自己不懂PHP,基本上都是用他人做好的插件,但这些插件总是不能满足自己想要的效果,于是自己常常瞎折腾。抄来抄去,最近几天终于折腾出一个自己的插件能满足自己的需求。