WordPress文章同步到微博头条

Gavin Wu 2022年03月20日 8:03 Wordpress 1,580 Views

自从新浪微博接口改版以后,也就没折腾同步文章到新浪微博。这段时间无聊又把wordpress博客折腾了一翻,又想起这事了。老规矩在网上先搜一搜。大致还是两种方法,插件和集成代码

先说插件

网上应该有不少这样的插件,这里推荐一款WordPres 同步微博插件
1、安装:插件页搜索WordPres 同步微博,安装即可。
2、使用:这里要有新浪开放平台的APP Key和APP Secret。

3、验证:将插件设置页面的授权回调页 代码填写到应用信息—高级信息-OAuth2.0 授权设置里的回调页和取消回调页里。然后wordpress插件页进行验证

4、完成

再说代码实现

1、应用要有文章头条高级接口权限

2、在主题function.php添加以下代码
下面8.9.10行要根据自己应用的情况修改,56行也可不修改,但发文章必须要有图片,不然会出错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* WordPress 同步文章到新浪微博头条文章 */
function post_to_sina_weibo_toutiao($post_ID) {
//ini_set('display_errors', true);
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 ($get_post_info->post_status == 'publish' && $_POST['original_post_status'] != 'publish')
{
$appkey = 'App Key';          //App Key
$username = '用户名';        //用户名
$userpassword = '密码';    //密码
$request = new WP_Http;
/* 获取文章标签关键词*/
$tags = wp_get_post_tags($post_ID);
foreach ($tags as $tag ) {
$keywords = $keywords.'#'.$tag->name."#";
}
$status = '【' . strip_tags($get_post_title) . '】 ' . mb_strimwidth(strip_tags(apply_filters('the_content', $get_post_centent)) , 0, 132, ' ');
$api_url = 'https://api.weibo.com/proxy/article/publish.json';
$body = array(
'title' => strip_tags($get_post_title), //头条的标题
'content' => get_post($post_ID)->post_content.'
原文地址:'
. get_permalink($post_ID), //头条的正文
'cover' => mmimg($post_ID), //头条的封面
'summary' => mb_strimwidth(strip_tags(apply_filters('the_content', $get_post_centent)) , 0, 110, '...'), //头条的导语
'text' => mb_strimwidth(strip_tags(apply_filters('the_content', $get_post_centent)) , 0, 110, $status).$keywords.'原文地址:' . get_permalink($post_ID), //微博的内容
'source' => $appkey
);
$headers = array('Authorization' => 'Basic ' . base64_encode("$username:$userpassword"));
$result = $request->post($api_url, array('body' => $body,'headers' => $headers));
logInfo($result['body']);
}
}
add_action('publish_post', 'post_to_sina_weibo_toutiao', 0); //给发布文章增加一个分享微博头条文章的动作
//获取封面
    function catch_that_image($postID){
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src="?(.+\.(jpg|gif|bmp|bnp|png))"?.+>/i',get_post($postID)->post_content,$matches);
    $first_img = $matches[1][0];
    //将文章第一张图片的地址赋值给$first_img
    if(empty($first_img)){
    //文章第一张图为空,也就是整篇文章没有图片,将默认设置的图片的地址赋值给$first_img
    $popimg = git_get_option('git_sina_weibo_cover');
    $first_img = $popimg;
    }
    return $first_img;
    }
    function mmimg($postID){
    $cti = catch_that_image($postID);
    //得到$first_img 的值,并赋值给$cti
    $showimg = $cti;
    //将$cti 的值赋值给$showimg
    has_post_thumbnail();
    if(has_post_thumbnail()){
    //判断是否有特色图片,有则将$showimg 的值替换为特色图片的地址,否则不变
    $thumbnail_image_url = wp_get_attachment_image_src(get_post_thumbnail_id(),'thumbnail');
    $shareimg = $thumbnail_image_url[0];
    }
    else{
    $shareimg = $showimg;
    }
    ;
    return $shareimg;
    }
    //调用代码:mmimg($post_ID)
    //写日志函数
    function logInfo($msg)
    {
    $logSwitch = 1; // 日志开关:1 表示打开,0 表示关闭
    $logFile = '/tmp/sync_weibo.log'; // 日志路径
    if ($logSwitch == 0 ) return;
    date_default_timezone_set('Asia/Shanghai');
    file_put_contents($logFile, date('[Y-m-d H:i:s]: ') . $msg . PHP_EOL, FILE_APPEND);
    return $msg;
    }

以上代码已经过测试,确保目前可以使用

代码来自祭夜の咖啡馆

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Top