欢迎来到资源库(www.zyku.net)

WordPress

当前位置:首页 > CMS教程 > WordPress > 自动同步

wordpress发布文章后自动同步图文到新浪微博的方法

时间:2017-03-07|栏目:WordPress|点击:|我要投稿

经常上新浪微博并且关注了不少官网认证号的小伙伴一定会发现很多官方性质的微博都会把网站的最新文章或者动态同步到新浪微博,这样一来就可以让微博用户快速的了解到网站的最新动态,并且也为网站增加了一个曝光以及流量入口,对于更新频繁的网站来说增加一个同步网站动态到微博的功能是非常有必要的,下面直接上代码:

class sync_sina {
  public $access_token = "";//access_token
  public $default_image = "";//默认图片地址
  public $host = "https://api.weibo.com/2/";
  public static $boundary = '';
  function __construct(){
  /**
  ** 事件绑定
  **/
  add_action('publish_post', array($this, 'new_post_photo'));
  }
  
  function do_mu_post($url, $data) {
  $ch = curl_init ();
  $headers = array("Content-Type:multipart/form-data;boundary=". self::$boundary);
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
  curl_setopt ( $ch, CURLOPT_POST, TRUE );
  curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
  curl_setopt ( $ch, CURLOPT_URL, $url );
  curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  $ret = curl_exec ( $ch );
  curl_close ( $ch );
  return $ret;
  }
  
  function build_http_query_multi($params) {
  if (!$params) return '';
  uksort($params, 'strcmp');
  $pairs = array();
  self::$boundary = $boundary = uniqid('------------------');
  $MPboundary = '--'.$boundary;
  $endMPboundary = $MPboundary. '--';
  $multipartbody = '';
  foreach ($params as $parameter => $value) {
  if( in_array($parameter, array('pic', 'image')) && $value{0} == '@' ) {
  $url = ltrim( $value, '@' );
  $content = file_get_contents( $url );
  $array = explode( '?', basename( $url ) );
  $filename = $array[0];
  $multipartbody .= $MPboundary . "\r\n";
  $multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $filename . '"'. "\r\n";
  $multipartbody .= "Content-Type: image/unknown\r\n\r\n";
  $multipartbody .= $content. "\r\n";
  } else {
  $multipartbody .= $MPboundary . "\r\n";
  $multipartbody .= 'content-disposition: form-data; name="' . $parameter . "\"\r\n\r\n";
  $multipartbody .= $value."\r\n";
  }
  }
  $multipartbody .= $endMPboundary;
  return $multipartbody;
  }
  
  function get_image($post_id){
  if( has_post_thumbnail($post_id) ){
  $timthumb_src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id),'full');
  $output = $timthumb_src[0];
  } else {
  $content = get_post_field('post_content', $post_id);
  $defaltthubmnail = $this->default_image;
  preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
  $n = count($strResult[1]);
  if($n > 0){
  $output = $strResult[1][0];
  } else {
  $output = $defaltthubmnail;
  }
  }
  return $output;
  }
  
  function new_post_photo($post) {
  global $post;
  if( $post->post_status != "publish" ){
  $token = $this->access_token;
  $url = $this->host ."statuses/upload.json";
  $status = "我刚刚发布了新文章《".get_the_title()."》。".get_permalink();
  $status .= mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0,180,"..."); //顺手加了个文章摘要,不喜欢就去掉啦
  $pic_path = self::get_image($post->ID);
  $params = array();
  $params['access_token'] = $token;
  $params['status'] = $status;
  $params['pic'] = '@'.$pic_path;
  $body = self::build_http_query_multi($params);
  $result = self::do_mu_post($url,$body);
  
  }
  }
  }
  $HMT = new sync_sina();

将以上代码添加至主题functions.php文件即可。

注:

需要服务器支持file_get_contents函数以及curl组件;

如果网站在国外服务器或者服务因特殊原因屏蔽了api.weibo.com域名,则可能造成发布文章卡死或者超时的问题。

如果网站未通过新浪审核微博小尾巴会显示为未审核应用。

(资源库 www.zyku.net)

关于我们 | 版权申明 | 寻求合作 |

重要申明:本站所有的文章、图片、评论等内容,均由网友发表或上传并维护或收集自网络,仅供个人学习交流使用,版权归原作者所有。

如有侵犯您的版权,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:95148658 | 邮箱:mb8#qq.com(#换成@)

苏ICP备2020066115号-1

本网站由提供CDN加速/云存储服务