一直以来我博客基本不传播,都是默默的放在各个网站或者社交账号的个人介绍那里,早些时候博客发文章的时候,在[无觅]相关文章中勾选绑定微博,可以同步到微博,缺点是经常要重新绑定,有时间限制吧,现在再打开这个网站直接挂了。
网上搜了一下有博客同步到微博的WP插件,但是大部分是卖钱的,还有卖100+的,有免费的,但是必须在他们网站注册一下,真的是杠杠的。
更新记录
2018-3-7 13:36:59 ,更新字数显示,最多显示138个字,微博中两个字母一个字,一个汉字一个字,链接占10个字。不过超过140个字也没什么关系。
参考文章
搜索的时候也发现有通过代码实现的:
[[API使用代码]WordPress自动同步到新浪微博头条文章]
[将WordPress中的文章自动同步到新浪微博(纯代码,支持自定义Appkey)]
分析
查看文章,基本都是通过微博的接口来做的,然后需要创建小应用,好麻烦。揉脸.gif。
OK,然后查看官方接口,发现只有一个可以用。[第三方分享链接到微博]。
好吧,那就创建应用,使用微博接口来实现,wordpress是php代码写的,所以可以查看[官方PHP SDK]
创建应用
在[微博·开发平台]中登陆微博账号,点击【微服务】,【立即创建微服务应用】,选【粉丝服务平台】(我是个人开发者,好像只能选这个)。按照步骤一步步设置。
确保以下要素:
- 应用名称:这个如果审核通过的话,同步到微博的时候,左下角那个来自XXXX,就是应用名称。
- App Key:很关键,后期使用。
- 应用地址:这个无所谓吧。
- 安全域名:这个很关键,接口中提到:文本中必须包含至少一个第三方分享到微博的网页URL,且该URL只能是该第三方(调用方)绑定域下的URL链接,绑定域在“我的应用 - 应用信息 - 基本应用信息编辑 - 安全域名”里设置。例如:laycher.com,www.laycher.com,可以不加http,但必须是你的博客域名。
- 其它随意。
另外,在【我的应用】-【具体应用】-【接口管理】-【已有权限】中需要保证有第三方分享到微博,我在16年创建的应用没有这个接口。
实现代码
测试过程中可以使用POSTMAN这个工具来测试,同时测试发现不用OAuth2.0授权方式也可以,改用Basic的方式。
在wordpress目录中找到当前使用主题的functions.php文件,例如:wordpress\wp-content\themes\twentyseventeen\functions.php,在最后(?>之前)加入以下代码,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | /** * WordPress文章同步到新浪微博,带图片 * 作者地址: http://laycher.com */ function post_to_sina_weibo($POST_ID, $debug = false) { $api_url = 'https://api.weibo.com/2/statuses/share.json'; $app_key = '1234567890'; //修改为自己的app key $user_pwd = 'dXNlcm5hbWVAMTYzLmNvbTpQYXNzd29yZA=='; //修改为base64位加密的 账号:密码,username@163.com:Password , 可到http://base64.xpcha.com/ 加密 if (wp_is_post_revision($POST_ID)) return; //修订版本不发布。 $post_status = get_post($POST_ID)->post_status; $post_title = get_post($POST_ID)->post_title; $post_content = get_post($POST_ID)->post_content; if ($post_status == 'publish' && $_POST['original_post_status'] != 'publish') { $status = '【' . strip_tags($post_title) . '】 ' . get_permalink($POST_ID) . mb_strimwidth(strip_tags(apply_filters('the_content', $post_content)) , 0, 150, '...'); $status = mb_strimwidth($status, 0, 128*2+mb_strlen(get_permalink($POST_ID),'utf-8'),'...','utf-8');//链接算十个字,共140个字,...算2个字 preg_match_all('/<img .+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+/>/i', $post_content, $matches); $first_img = $matches[1][0]; //将文章第一张图片的地址赋值给$first_img if (empty($first_img)) { // 文章第一张图为空, $request = new WP_Http; $headers = array( 'Authorization' => 'Basic ' . $user_pwd, 'Content-Type' => 'application/x-www-form-urlencoded' ); $body = array( 'source' => $app_key, 'status' => $status ); $result = $request->post($api_url, array( 'body' => $body, 'headers' => $headers )); if ($debug) { logInfo($result['body']); } } else { $array = explode('?', basename($first_img)); $filename = $array[0]; $filecontent = file_get_contents($first_img); $boundary = uniqid('------------------'); $MPboundary = '--' . $boundary; $endMPboundary = $MPboundary . '--'; $multipartbody = ''; $multipartbody.= $MPboundary . "\r\n"; $multipartbody.= 'content-disposition: form-data; name="source' . "\"\r\n\r\n"; $multipartbody.= urlencode($app_key) . "\r\n"; $multipartbody.= $MPboundary . "\r\n"; $multipartbody.= 'content-disposition: form-data; name="status' . "\"\r\n\r\n"; $multipartbody.= urlencode($status) . "\r\n"; $multipartbody.= $MPboundary . "\r\n"; $multipartbody.= 'Content-Disposition: form-data; name="pic"; filename="' . $filename . '"' . "\r\n"; $multipartbody.= "Content-Type: image/unknown\r\n\r\n"; $multipartbody.= $filecontent . "\r\n"; $multipartbody.= $endMPboundary; $headers = array( 'Authorization: Basic ' . $user_pwd, 'content-Type: multipart/form-data; boundary=' . $boundary ); $ci = curl_init(); curl_setopt($ci, CURLOPT_URL, $api_url); curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); //关闭SSL,否则校验证书失败 curl_setopt($ci, CURLOPT_POST, TRUE); curl_setopt($ci, CURLOPT_POSTFIELDS, $multipartbody); $curlResponse = curl_exec($ci); $err = curl_error($ci); curl_close($ci); if ($debug) { logInfo($curlResponse); if ($err) { logInfo("cURL Error #:" . $err); } } } } } // 写日志函数 function logInfo($msg) { $path = get_template_directory(); $logFile = $path.'/sync_weibo.log'; // 日志路径 date_default_timezone_set('Asia/Shanghai'); file_put_contents($logFile, date('[Y-m-d H:i:s]: ') . $msg . PHP_EOL, FILE_APPEND); return $msg; } add_action('publish_post', 'post_to_sina_weibo', 0); //0代表优先级,默认10,最小。 |
上述$debug改为true的话,就会在functions.php的同目录下生产sync_weibo.log文件,查看执行结果日志。
效果展示
发完这篇文章,去我的微博看看吧。[Laycher的微博]
>> 若为原创,转载请注明: 转载自Laycher's Blog
>> 本文链接地址: WordPress发布文章自动同步到新浪微博
>> 订阅本站: http://feed.feedsky.com/laycher
有空照你的样子弄一下,谢谢了。
我的搞了两天都没成功