首先介绍一下这个插件,wp-to-weibo就是WordPress同步到新浪微博的插件。之前很喜欢用朋友圈,可朋友真的多起来的时候,我又喜欢用微博了,或许哪天微博朋友多起来了,我又喜欢了Twitter。
前两年这个功能很多人用,现在用WordPress的人都没那么多了,好多这种插件代码的朋友都是N年前更新的。不过这代码又不是没用的。
这里要感谢张戈的博客,他里面介绍了很多关于怎么同步到新浪微博的方法,怎么去发现问题,受益良多。而且他之前也做了这样的一个插件。所以我这个插件基本上也是从他的代码中提炼出来的。
从根本上说不是他的代码有问题,是微博的接口问题,之前他介绍的微博高级写入接口,我找了半天都没找到,最后只有头条文章高级写入接口比较符合,如果想申请头条文章发布的权限,好像还需要至少有发布过10篇以上这样的文章才行。不过从微博开发平台里申请微连接之后单独申请这样的接口就OK了。
但是我这边插件除了支持这个接口,还有一个接口还可以同步到微博,那就是分享接口——第三方分享接口,这个接口只要你申请微连接默认是有的。他同步的形式如上图所示,这几天我测试了N遍。
主要核心代码有两个:一是分享到微博的形式:
function post_to_sina_weibo($post_ID) {
if( wp_is_post_revision($post_ID) ) return;
$get_post_info = get_post($post_ID);
$get_post_centent = get_post($post_ID)->post_content;
$get_post_title = get_post($post_ID)->post_title;
if(!empty(get_post_format($post_ID))){
if(get_post_format($post_ID) == 'aside') $get_post_format = __('aside','wp-to-weibo');
if(get_post_format($post_ID) == 'gallery') $get_post_format = __('gallery','wp-to-weibo');
if(get_post_format($post_ID) == 'link') $get_post_format = __('link','wp-to-weibo');
if(get_post_format($post_ID) == 'image') $get_post_format = __('image','wp-to-weibo');
if(get_post_format($post_ID) == 'quote') $get_post_format = __('quote','wp-to-weibo');
if(get_post_format($post_ID) == 'status') $get_post_format = __('status','wp-to-weibo');
if(get_post_format($post_ID) == 'video') $get_post_format = __('video','wp-to-weibo');
if(get_post_format($post_ID) == 'audio') $get_post_format = __('audio','wp-to-weibo');
if(get_post_format($post_ID) == 'chat') $get_post_format = __('chat','wp-to-weibo');
}else{
$get_post_format = __('Headline', 'wp-to-weibo');
}
if ($get_post_info->post_status == 'publish') {
$appkey = "新浪微博APPKEY";
$username = “新浪微博账号”;
$userpassword = “新浪微博密码”;
$request = new WP_Http;
$keywords = "";
/* keywords of post */
$tags = wp_get_post_tags($post_ID);
foreach ($tags as $tag ) {
$keywords = sprintf(__(' %1$s # %2$s # ', 'wp-to-weibo'), $keywords, $tag->name);
}
/* add keywords to post topic*/
$string1 = sprintf(__('【 %1$s 】 %2$s : ','wp-to-weibo'), strip_tags($get_post_format), strip_tags( $get_post_title ));
$string2 = sprintf(__('%1$s Read More %2$s', 'wp-to-weibo'), $keywords, get_permalink($post_ID));
/* post num, sync failed if more */
$wb_num = (138 - WeiboLength_test($string1.$string2))*2;
$status = $string1.mb_strimwidth(strip_tags( apply_filters('the_content', $get_post_centent)),0, $wb_num,'...').$string2;
/* get the thumbnail, or get the first pic */
$url = get_mypost_thumbnail($post_ID);
//$url = preg_replace('/https:\/\//i','http://',$url);
$api_url = 'https://api.weibo.com/2/statuses/share.json'; /* API */
$body = array('status' => $status,'source' => $appkey,'pic' => $url);
$headers = array('Authorization' => 'Basic ' . base64_encode("$username:$userpassword"));
$result = $request->post($api_url, array('body' => $body,'headers' => $headers));
return json_encode($result);
}
}
另一种是以头条文章的形式:
function post_to_sina_toutiao($post_ID) {
if( wp_is_post_revision($post_ID) ) return;
$get_post_info = get_post($post_ID);
$get_post_centent = get_post($post_ID)->post_content;
$get_post_title = get_post($post_ID)->post_title;
$get_the_excerpt = get_post($post_ID)->post_excerpt;
if(!empty(get_post_format($post_ID))){
if(get_post_format($post_ID) == 'aside') $get_post_format = __('aside','wp-to-weibo');
if(get_post_format($post_ID) == 'gallery') $get_post_format = __('gallery','wp-to-weibo');
if(get_post_format($post_ID) == 'link') $get_post_format = __('link','wp-to-weibo');
if(get_post_format($post_ID) == 'image') $get_post_format = __('image','wp-to-weibo');
if(get_post_format($post_ID) == 'quote') $get_post_format = __('quote','wp-to-weibo');
if(get_post_format($post_ID) == 'status') $get_post_format = __('status','wp-to-weibo');
if(get_post_format($post_ID) == 'video') $get_post_format = __('video','wp-to-weibo');
if(get_post_format($post_ID) == 'audio') $get_post_format = __('audio','wp-to-weibo');
if(get_post_format($post_ID) == 'chat') $get_post_format = __('chat','wp-to-weibo');
}else{
$get_post_format = __('Headline', 'wp-to-weibo');
}
if ($get_post_info->post_status == 'publish') {
$appkey = "新浪微博APPKEY";
$username = “新浪微博账号”;
$userpassword = “新浪微博密码”;
$request = new WP_Http;
$keywords = "";
/* keywords of post */
$tags = wp_get_post_tags($post_ID);
foreach ($tags as $tag ) {
$keywords = sprintf(__(' %1$s # %2$s # ', 'wp-to-weibo'), $keywords, $tag->name);
}
/* add keywords to post topic*/
$string1 = sprintf(__('【 %1$s 】 %2$s : ','wp-to-weibo'), strip_tags($get_post_format), strip_tags( $get_post_title ));
$string2 = sprintf(__('%1$s Read More %2$s', 'wp-to-weibo'), $keywords, get_permalink($post_ID));
/* post num, sync failed if more */
$status = $string1.$get_post_centent.$string2;
/* get the thumbnail, or get the first pic */
$url = get_mypost_thumbnail($post_ID);
//$url = preg_replace('/https:\/\//i','http://',$url);
$api_url = 'https://api.weibo.com/proxy/article/publish.json';
$body = array('title' => $get_post_title, 'text' => $get_the_excerpt, 'summary' => $get_the_excerpt, 'content' => $status, 'source' => $appkey, 'cover' => $url);
$headers = array('Authorization' => 'Basic ' . base64_encode("$username:$userpassword"));
$result = $request->post($api_url, array('body' => $body,'headers' => $headers));
return json_encode($result);
}
}
其实两段代码基本一样,不同的是接口。另外,我还添加了post-format类型。如果不满意我的中文翻译,可以用poedit软件重新翻译,当然也可以反馈给我,我会考虑您的意见。
此插件刚已经提交到官方插件库了,不知道什么时候可以审核通过。目前给大家一个下载地址:WordPress插件wp-to-weibo
注意:如果以文章头条的形式,excerpt,即摘要内容必须有,否则同步失败。因为我把微博和导语部分是同步摘要的部分。其实这个插件做的不完美,特别是头条文章的形式,文章打开之后,显示的是格式化的文章,没有段落。有高手路过,可以帮我一起完善它,感谢!
== 已知bug ==
1、如上所述,头条文章形式,打开文章无格式,无段落;
2、文章更新后,还是会同步到微博,这个虽然可以加字段解决,但是我又不想在数据库里加个字段,而官方这个wp_is_post_revision貌似也不管用!
== changelog ==
2018.02.08 开始提交该插件;
2018.02.10 官方审核通过!
本文链接: WordPress插件:wp-to-weibo
小刘真是太强大了。
7年前 · 嘉兴 · Google Chrome 55 Windows 7
无法启用插件,因为它引起了一个致命错误(fatal error)。
Fatal error: Can’t use function return value in write context in /**************.com/wp-content/plugins/wp-to-weibo/class-weibo.php on line 11
7年前 · 苏州 · Google Chrome 64 Mac OS X 10.13
@yidilian 不应该啊,这一行不是自定义函数啊!
是插件冲突么?
7年前 · 合肥 · Firefox 58 Windows 10
WordPress 4.9.4,使用Sequential主题,同步失败,看了博主的源代码,感觉没有任何问题,但就是同步不了,很奇怪。
跟着微博的官方文档写了下面这个函数,用开发者token在本地可以成功分享,但是到了服务器上的WordPress用钩子却无法同步了,头痛。
不知道在WordPress上有没有办法知道它发送post请求后的response,如果能得到response或许能知道同步不了的原因。
function post_to_sina_weibo($status, $pic, $token) {
$url=’https://api.weibo.com/2/statuses/share.json’;
$status = urlencode($status);
$options = array(
‘status’ => $status,
‘access_token’ => $token,
‘pic’ => new \CurlFile($pic, ‘image/png’, ‘1.png’)
);
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $options);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
}
7年前 · 广州 · Google Chrome 64 Mac OS X 10.12
@Ckend 原来是位大神,这代码我看不懂,你可以参考一下https://zhangge.net/5082.html这篇文章,他可以测试问题。
7年前 · 合肥 · QQbrowser 10 Windows 7
@皇家元林 噢,昨晚一个插件搞冲突了,挂了一段时间。话说我找到原因了,建议用save_post钩子代替publish_post. 更加兼容一些网站。
7年前 · 广州 · Google Chrome 64 Mac OS X 10.12
@Ckend 嗯嗯,你也可以考虑下transition_post_status这个,刚试了一下,可以解决更新文章时不同步得问题!
7年前 · 合肥 · Firefox 58 Windows 10
@Ckend 另外,你的站貌似挂了,打不开啊
7年前 · 合肥 · QQbrowser 10 Windows 7
我发了篇文章貌似没同步成功
6年前 · 济南 · Firefox 64 Windows 10
@haidemeng 你是用插件还是代码呢?
6年前 · 合肥 · Firefox 64 Windows 10
@皇家元林 我用的插件
6年前 · 济南 · Internet Explorer 11 Windows 10
@haidemeng 不是光用插件,还得有新浪微博API接口的权限
6年前 · 合肥 · Firefox 64 Windows 10
有的已经审核通过了,
6年前 · 济南 · Internet Explorer 11 Windows 10
@haidemeng 刚加群里的是不是你啊,你在群里找我一下,我帮你看看咋回事
6年前 · 合肥 · Firefox 64 Windows 10
感谢老大。
6年前 · 济南 · Internet Explorer 11 Windows 10
This plugin doesn’t work at all.
5年前 · Bayan通讯公司用户 · Google Chrome 77 Windows 8.1
@Richard what’s wrong?
I’m using it now, and it’s OK!
Do you have an account for weibo.com?
and you must have the appkey of the website!
5年前 · 河北 · Firefox 69 Windows 7
@皇家元林 哈哈哈。。英文非常好的么
5年前 · 嘉兴 · Google Chrome 63 Windows 10
@木木 初中英语,献丑了
5年前 · 廊坊 · Google Chrome 77 Windows 7
Hi 皇家元林
Thanks for your reply. Yes I have a Weibo account and a Weibo app. In the settings of WP to Weibo my Weibo name is Rolex-Watch and the App key is 2440656683. What I understand from your plugin is that it shares posts to Weibo automatically? Is that correct? Anyway, when I make a test post, nothing is posted to my Weibo page https://www.weibo.com/realrolexwatches Maybe you like to test it yourself? I can give you my login information if needed. If you need it I can send it to your email address. Thanks!
Kind regards
Richard
5年前 · Bayan通讯公司用户 · Google Chrome 77 Windows 8.1
@Richard my pleasure!
I am glad you are using weibo in China.
If you trust me, send your login account and password by email.
i help you test it.
best wish!
5年前 · 河北 · Firefox 69 Windows 7
Hi 皇家元林
I am from the Netherlands and my native language is Dutch. I cannot speak Chinese but I would like to learn it. The last 20 years I have been living in the Philippines so that’s where I live now. Before I give you the login info, may I know what is call back page in my App? I filled in https://richarddetering.com/wp-admin/options-general.php?page=wp2wb-options Is that correct?
5年前 · Bayan通讯公司用户 · Google Chrome 77 Windows 8.1
https://richarddetering.com/wp-admin/options-general.php?page=wp-to-weibo
you can see the options.
no call back page, and only an admin option page.
I see.
is the plugin you use not mine?
my plugin name is wp-to-weibo.
5年前 · 河北 · Firefox 69 Windows 7
Am using wp-to-weibo. Please check your email. Thanks.
5年前 · Bayan通讯公司用户 · Google Chrome 77 Windows 8.1
好像不起作用,使用分享模式,三个选项包括登陆账号,登陆密码,以及APP-KEY;发布了两篇都不行;不知道是不是用户名,不是填电子邮箱的的登陆账号,而是填写微博名?
5年前 · 厦门 · Opera 64 Windows 10
@连先生 是登录用户名和密码和APPkey
5年前 · 河北 · Wordpress App 13 iPhone iOS 13.2