马春杰杰 Exit Reader Mode

[mcj]WordPress增加自动发表微博头条

上代码:

/** 
 * WordPress 同步文章到新浪微博头条文章 By 无主题博客 
 * 完善修正 By 祭夜
 * 修正内容:
 * 1.部分代码错误
 * 2.修复同步到头条文章时HTML代码被去掉的问题
 * 3.增加头条文章封面
 * 4.添加博客文章的标签关联到新浪话题
 * 原文地址: http://wuzhuti.cn/2715.html(原站已停运)
 */  
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='1484874782'; /* 此处是你的新浪微博appkey,不修改的话就会显示来自张戈博客哦! */
       $username='XXX';
       $userpassword='XXX';

    $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)

 

本文最后更新于2019年7月8日,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!