Wordpress文章同步到微博方法与代码

微博在新版api发布以后,不能像以前随意申请完api后自动同步wordpress的文章,同时ifttt也不支持同步文章到微博上。因此必须重新配置。步骤如下: 1,到http://open.weibo.com/apps申请一个新的api,申请的时候要有身份证正反面照片才能通过。 2,审核通过后,申请高级权限https://api.weibo.com/proxy/article/publish.json,否则只能用https://api.weibo.com/2/statuses/share.json同步。 3,修改functions.php代码,插入如下代码:
function post_to_sina_weibo_toutiao($post_ID) { //ini_set('display_errors', true); if(wp_is_post_revision($post_ID)) return; if(get_post_meta($post_ID,'weibo_sync',true) == 1) 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 = '1921963410'; $username = '你的账号'; $userpassword = '你的密码'; $request = new WP_Http;   $keywords = ""; $post_categories = wp_get_post_categories( $post_id ); foreach($post_categories as $c){ $cat = get_category( $c ); $keywords = $keywords.'#'.$cat->name."#"; }   // $tags = wp_get_post_categories($post_ID); // foreach ($tags as $tag ) { // $keywords = $keywords.'#'.$tag->name."#"; // } $status = '「' . strip_tags($get_post_title) . '」:' .' 原文链接:'. get_permalink($post_ID).' '. mb_strimwidth( str_replace(PHP_EOL, '',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, 'source' => $appkey ); $headers = array('Authorization' => 'Basic ' . base64_encode("$username:$userpassword")); $result = $request->post($api_url, array('body' => $body,'headers' => $headers)); logInfo($result['body']); $msg = json_decode($result['body']); $code = $msg->code; if($code != "100000"){ $api_url2 = "https://api.weibo.com/2/statuses/share.json";   $pic_url = mmimg($postID); $pic=file_get_contents($pic_url );   $body2 = array('status' => $status,'pic' => $pic,'source' => $appkey); $result2 = $request->post($api_url2, array('body' => $body2,'headers' => $headers)); logInfo2($result2['body']); }   add_post_meta($post_ID, 'weibo_sync', 1, true); } } add_action('publish_post', 'post_to_sina_weibo_toutiao', 0);   function mmimg($postID) { if (has_post_thumbnail()) { $timthumb_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_ID), 'full' ); $url = $timthumb_src[0]; } else { if(!$post_content){ $post = get_post($post_ID); $post_content = $post->post_content; } preg_match_all('//i',$post->post_content, $matches); if( $matches && isset($matches[1]) && isset($matches[1][0]) ){ $url = $matches[1][0]; }else{ $url = ''; } } return $url; }   function logInfo2($msg) { $logSwitch = 1; $logFile = '/tmp/sync_weibo_no_toutiao.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; }   function logInfo($msg) { $logSwitch = 1; $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; }
  这样,当你没有高级同步头条文章权限的时候,也可以用低级权限同步文章,只是没有办法成为头条文章。
← 返回文章列表