分享同步长微博代码,免插件文章同步到微博

记录下调试的微博同步代码,有需要的欢迎拿走。先来个效果图。

微博同步
微博同步

代码如下,直接丢在函数模版functions.php即可,注意修改帐号密码

/** 
 * WordPress 同步文章到新浪微博头条文章 By 无主题博客 
 */  
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 = '输入自己的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() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$post->post_content, $matches);
$first_img = $matches [1] [0];             //将文章第一张图片的地址赋值给$first_img
if(empty($first_img)){                          //文章第一张图为空,也就是整篇文章没有图片,将默认设置的图片的地址赋值给$first_img
$popimg= '默认图片地址(可以是 LOGO 等)';
$first_img = "$popimg";}
return $first_img;}
function mmimg($postID) {
 $cti = catch_that_image();                  //得到$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;
}
//微博头条同步结束

ok保存就可以了。以上代码来自:喵喵喵博客 https://www.miaomiaomiao.org/10126.html

分享同步长微博代码,免插件文章同步到微博》有1个想法

  1. Pingback引用通告: 由于折腾的比较多,创建了WP子主题 | 设若

发表回复

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