如下方法时间有点久远吧,不知道是否有效。但可以作为学习publish_post 发布文章钩子的一个典型例子!

自从新浪微博接口调整后,本站就没有使用同步功能,前几天无意看到同步到新浪微博头条文章,于是就弄到自己站,改了点代码。同步失败不会创建文件。

感谢张戈博客提供的初始代码、无主题博客的头条代码,祭夜的代码(wp5.3版本经过测试无法同步文章图片到头条封面)

1.代码增加:同步失败在文章自定义栏目weibo_sync增加错误代码,同步成功则增加weibo_sync字段1。
2.修复部分文章发布报:param text invalid。
3.修复头条封面显示默认图片的问题。

使用微博头条文章同步首先要申请微博的头条文章高级写入接口。

20200204152835.jpg



接口文档地址:https://open.weibo.com/wiki/Toutiao/api

请求URL:https://api.weibo.com/proxy/article/publish.json
提交成功返回:

{"code":100000,"msg":"","data":{"object_id":"1022:2310474468279925145779","url":"http://weibo.com/ttarticle/p/show?id=2310474468279925145779","mid":4468279927839992}}

用户名或密码错误失败返回:

{"request":"/proxy/article/publish.json","error_code":"21302","error":"username or password error!"}

发布太频繁会返回110045:

{"code":110045,"msg":"u53d1u5e03u5931u8d25,u8bf7u7a0du540eu91cdu65b0u53d1u5e03","data":[]}

把14,15,16行修改成你自己的账号信息。


把下面的代码放入主题文件functions.php

/**
 * WordPress 自动同步文章到新浪微博头条文章 By 讯沃blog
 * 修改内容:同步失败在文章自定义栏目weibo_sync增加错误代码
 * 地址: https://www.77nn.net/2570.html
 */
function traum_toutiao_publish($post_ID) {
 /* 此处修改为通过文章自定义栏目来判断是否同步 */
 if(get_post_meta($post_ID,'weibo_sync',true)) 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 = '微博开放平台的appkey';
 $username = '微博用户名';
 $password = '微博密码';
        $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';
        //头条的标题
        $title = strip_tags($get_post_title);
        //头条的正文
        $content = get_post($post_ID)->post_content."rn原文:<a href=" . get_permalink($post_ID).">点击查看</a>";
        $content = traum_toutiao_handle_content($content);
        //头条的封面
        $first_img = get_mypost_thumbnail($post_ID);
        $cover = $first_img;
        //头条的导语
        $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);
        $body = array(
            'title' => $title,
            'content' => $content,
            'cover' => $cover,
            'summary' => $summary,
            'text' => $text,
            'source' => $appkey
        );
        $headers = array('Authorization' => 'Basic ' . base64_encode("$username:$password"));
    $result = $request->post($api_url, array('body' => $body,'headers' => $headers));
  $weibo_sync  = $result['body'];
  $result_text = '1';
  $weibo_code = json_decode($weibo_sync);
if( $weibo_code->error_code ){
   $result_text = '失败 '.$weibo_code->error;
 }
   // 若成功,则给新增自定义栏目weibo_sync,避免以后更新文章重复同步
   add_post_meta($post_ID, 'weibo_sync', $result_text, true);
    }
}
//给发布文章增加一个分享微博头条文章的动作
add_action('publish_post', 'traum_toutiao_publish', 0);
 
/**
* WordPress 获取文章图片加强版 By 张戈博客
*/
if(!function_exists('get_mypost_thumbnail')){
  function get_mypost_thumbnail($post_ID){
     if (has_post_thumbnail()) {
            $timthumb_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_ID), 'full' ); 
            $url = $timthumb_src[0];
    } else {
        if(!$post_content){
            $post = get_post($post_ID);
            $post_content = $post->post_content;
        }
        preg_match_all('|<img.*?src=['"](.*?)['"].*?>|i', do_shortcode($post_content), $matches);
        if( $matches && isset($matches[1]) && isset($matches[1][0]) ){       
            $first_img =  $matches[1][0];
        }else{
            $first_img =  '';
        }
    }
    return $first_img;
  }
}
//处理content内容
function traum_toutiao_handle_content($content) {
    if (!strpos($content, "<h1>") && strpos($content, "<h2>") && (strpos($content, "<h3>") || strpos($content, "<h4>") || strpos($content, "<h5>") ||strpos($content, "<h6>"))) {
        $content = str_replace("<h2>", "<h1>", $content);
        $content = str_replace("</h2>", "</h1>", $content);
    }
    $content = preg_replace("/[/?[a-z]+_[a-z]+]/","",$content);
    $content = str_replace(array("<br>", "<br />"), "&lt;br&gt;", $content);
    $content = str_replace(array("rn", "r", "n"), "<br>", $content);
    $content = str_replace('code>', "b>", $content);
    return $content;
}

发表回复

后才能评论