wordpress插件版
  [WP插件]WordPress头条文章插件
文章目录[隐藏] 代码版 设置界面: To Do 说明 项目 历时一天 #(流汗滑稽) 这个插件初步完成了。 […]
Typecho版
  [插件]typecho的微博头条同步插件
WordPress版看这里 本来不想写Typecho的头条文章插件,应该是没空写。。。因此我推辞 […]
更新日志
2019-07-18 停更代码,转向插件
插件继续更新,优化细节,增加微博官方的html标签的支持
2018-06-21 修正获取封面代码,更正一个数字
2017-09-01 我加了需要修改的地方的说明  
 
2017-08-17 修正同步话题
2017-08-13 添加博客文章的标签关联到新浪话题
2017-07-21 修复封面为第一张图片时,获取失败的问题
代码在文章底部
最近申请了微博登陆看见微博里有个头条文章高级写入接口
申请传送—>微博开放平台
祭夜比较懒,就去百度搜了搜,看见一个插件竟然要18RMB
纳尼?至于吗。。。。我一个穷高中生买不起啊
于是呢,我就看了看其他的
发现有个代码版,不错可以折腾折腾
有兴趣的去看看——–>传送门
原版的问题挺多,我就修改优化了
实现原理
在发布文章时,调用微博头条文章接口,实现博客文章与头条文章同步
接口文档简单说明
接口文档地址:http://open.weibo.com/wiki/2/proxy/article/publish
文档中使用 oauth2 授权方式,通过access_token 获得权限调用接口,为了适应Wordpress和代码编写简单,我们采用BaseAuth 授权方式。如果你对授权方式感兴趣,可以查看REST API 安全设计指南 这篇文章。
发布后效果
代码编写
注意:部分代码需要自行修改!!!
必须修改:19,20,21,55行
建议修改:72行(正常使用请关闭,异常时请打开)
以下代码增加到自己主题的functions.php文件中。
/** 
 * WordPress 同步文章到新浪微博头条文章 By 无主题博客 
 * 完善修正 By 祭夜
 * 修正内容:
 * 1.部分代码错误
 * 2.修复同步到头条文章时HTML代码被去掉的问题
 * 3.增加头条文章封面
 * 4.添加博客文章的标签关联到新浪话题
 * 原文地址:https://wuzhuti.cn/2715-html
 *修正地址:https://www.jysafe.cn/1940.air
 */  
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;
}
以上代码已经过祭夜测试,并以投入使用
有问题请在评论区反馈,谢谢

![[API使用代码]WordPress自动同步到新浪微博头条文章 [API使用代码]WordPress自动同步到新浪微博头条文章](https://cdn.jysafe.cn/wp-content/uploads/2017/07/2017071509502163.png!water.jpg)
![[API使用代码]WordPress自动同步到新浪微博头条文章 [API使用代码]WordPress自动同步到新浪微博头条文章](https://cdn.jysafe.cn/wp-content/uploads/2017/07/2017071509284497.png!water.jpg)
![[API使用代码]WordPress自动同步到新浪微博头条文章 [API使用代码]WordPress自动同步到新浪微博头条文章](https://cdn.jysafe.cn/wp-content/uploads/2017/07/2017081410430075.jpg!water.jpg)

![[记录]成都信息工程大学打卡登录操作](https://cdn.jysafe.cn/assets/images/pics/014.webp!githumb3.jpg)
![[记录]Github接入认证更新V3 Authorization HTTP](https://cdn.jysafe.cn/wp-content/uploads/2020/02/2020021013514870.png!githumb3.jpg)
![[WordPress插件]QQ微博百度GitHub微信登录插件 正式版](https://cdn.jysafe.cn/wp-content/uploads/2017/08/2020020515011360.png!githumb3.jpg)


错误码 错误信息 描述
10001 system error 系统错误
10008 param xxx invalid 参数xxx不符合要求
11001 sass check error 发布过于频繁
11002 create statuses error 微博发送失败
11003 bind error 文章关联微博失败
$popimg= '默认图片地址(可以是LOGO等)';
没有修改为正确的地址。