Warning: strpos(): Empty needle in /home/www/wosn.net/wp-content/themes/begin/inc/inc.php on line 1157
Warning: strpos(): Empty needle in /home/www/wosn.net/wp-content/themes/begin/inc/inc.php on line 1157
Warning: strpos(): Empty needle in /home/www/wosn.net/wp-content/themes/begin/inc/inc.php on line 1157
Warning: strpos(): Empty needle in /home/www/wosn.net/wp-content/themes/begin/inc/inc.php on line 1157
Warning: strpos(): Empty needle in /home/www/wosn.net/wp-content/themes/begin/inc/inc.php on line 1157
Warning: strpos(): Empty needle in /home/www/wosn.net/wp-content/themes/begin/inc/inc.php on line 1157
以前想把博客加个微博自动同步功能,在网上看了众多方法都是通过”微博高级写入接口“来实现,但这个接口已在六月关闭了无法申请,也就把这个想法放下了。后面在一次逛微博开放平台时发现了个”微博头条开放协议“,这不就是可以实现同步吗?百度一下,发现”祭夜博客“分享了这个接口实现的代码,经过测试确实可行。于是,沃森博客把”[纯代码]WordPress自动同步到新浪微博头条文章“的方法分享下,方便下别人也备份下代码。
1、申请微博头条文章高级写入接口
申请传送--->微博开放平台
![[纯代码]WordPress自动同步到新浪微博头条文章-图片1](http://pic.wosn.net/blog/170923/5BdH3eF1i2.png)
2、实现代码
半下代码加到当前主题的functions.php文件中。
PS:注意修改第9、10、11、44行数据,第62行建议关闭,异常时再打开。
- 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()?{
 - 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;
 - }
 
3、实现效果
至此,实现[纯代码]WordPress自动同步到新浪微博头条文章的方法就说完了。
PS:整理自https://www.jysafe.cn/1940.air

![[纯代码]WordPress自动同步到新浪微博头条文章-图片2](http://pic.wosn.net/blog/170923/0hEHiIh7b0.png)
评论