欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

wordpress文章自动同步到新浪微博[支持图片]

shiping1 的头像
一直想弄这个功能来着,奈何网上的收费插件太贵啦,就不停的百度,不停的百度,找到了无主题跟张戈博客中的博文,下面的代码也是借鉴他们2位的文章。再此鸣谢。同步到腾讯微博,请看http://www.ykuaile.net/archives/1036.html

准备工作

1.申请新浪微博开放平台

网站  网站接入流程

2.开通微博写入高级接口

在应用管理平台->选择接口管理->申请权限->选择“微博高级写入接口”->填写申请理由->等待一个工作日

部署代码

把下面代码加在主题的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
//同步新浪微博
function post_to_sina_weibo($post_ID) {
   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='1125374965'//此处是你的新浪微博appkey,不修改的话就会显示来自Share Ykuaile哦!]
       $username='您的新浪微博用户名';
       $userpassword='您的新浪微博密码';
       $request = new WP_Http;
       $keywords = "";
 
 
       //获取特色图片,如果没设置就抓取文章第一张图片
       if (has_post_thumbnail()) {
          $image_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_ID), 'large');
          $url = $image_src[0];
          } else{
 
          //抓取第一张图片作为特色图片,如果没有发表成功,则需要放入回收站,再次还原就可以发表照片了。
          global $post;
          $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',$post->post_content,$matches);
          if($output) $url = $matches[1][0];
       }
 
       //获取文章标签关键词
       $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,'...') .$keywords. ' 查看全文:' . get_permalink($post_ID) ;
        
        //判断是否存在图片,定义不同的接口
       if(!empty($url)){
           $api_url = 'https://api.weibo.com/2/statuses/upload_url_text.json'; //新的API接口地址
           $body = array('status' => $status,'source' => $appkey,'url' => $url);
       } else {
           $api_url = 'https://api.weibo.com/2/statuses/update.json';
           $body = array('status' => $status,'source' => $appkey);
       }
 
       $headers = array('Authorization' => 'Basic ' . base64_encode("$username:$userpassword"));
       $result = $request->post($api_url, array('body' => $body,'headers' => $headers));
 
    }
}
add_action('publish_post', 'post_to_sina_weibo', 0);

发布无图片代码

把下面代码加在主题的functions.php中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function post_to_sina_weibo($post_ID) {
    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='1125374965';
        $username='微博用户名';
        $userpassword='微博密码';
        $request = new WP_Http;
        $status = '【' . strip_tags($get_post_title) . '】 ' . mb_strimwidth(strip_tags(apply_filters('the_content', $get_post_centent)) , 0, 132, '...') . ' 全文地址:' . get_permalink($post_ID);
        $api_url = 'https://api.weibo.com/2/statuses/update.json';
        $body = array('status' => $status,'source' => $appkey);
        $headers = array('Authorization' => 'Basic ' . base64_encode("$username:$userpassword"));
        $result = $request->post($api_url, array('body' => $body,'headers' => $headers));
    }
}
add_action('publish_post', 'post_to_sina_weibo', 0);//给发布文章增加一个分享微博的动作
原文链接:,转发请注明来源!
来自 http://www.ykuaile.net/archives/983.html
普通分类: