經常上新浪微博並且關注了不少官網認證號的小夥伴一定會發現很多官方性質的微博都會把網站的最新文章或者動態同步到新浪微博這樣一來就可以讓微博使用者快速 的瞭解到網站的最新動態,並且也為網站增加了一個曝光以及流量入口,對於更新頻繁的網站來說增加一個同步網站動態到微博的功能是非常有必要的,也不扯淡 了,直接上程式碼:

  1. class sync_sina {
  2.     public $access_token = "";
  3.     public $default_image = "";
  4.     public $host = "https://api.weibo.com/2/";
  5.     public static $boundary = '';
  6.     function __construct(){
  7.         add_action('publish_post', array($this, 'new_post_photo'));
  8. function do_mu_post($url$data) {
  9.         $ch = curl_init ();
  10. $headers = array("Content-Type:multipart/form-data;boundary=". self::$boundary);
  11.         $ch = curl_init();
  12. $ch, CURLOPT_HTTPHEADER, $headers);
  13.         curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
  14. $ch, CURLOPT_POST, TRUE );
  15.         curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
  16. $ch, CURLOPT_URL, $url );
  17.         curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  18. $ret = curl_exec ( $ch );
  19.         curl_close ( $ch );
  20. return $ret;
  21.     }
  22.     function build_http_query_multi($params) {
  23. if (!$paramsreturn '';
  24.         uksort($params, 'strcmp');
  25. $pairs = array();
  26.         self::$boundary = $boundary = uniqid('------------------');
  27. $MPboundary = '--'.$boundary;
  28.         $endMPboundary = $MPboundary. '--';
  29. $multipartbody = '';
  30.         foreach ($params as $parameter => $value) {
  31. if( in_array($parameterarray('pic', 'image')) && $value{0} == '@' ) {
  32.                 $url = ltrim( $value, '@' );
  33. $content = file_get_contents$url );
  34.                 $array = explode( '?', basename$url ) );
  35. $filename = $array[0];
  36.                 $multipartbody .= $MPboundary . "rn";
  37. $multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $filename . '"'. "rn";
  38.                 $multipartbody .= "Content-Type: image/unknownrnrn";
  39. $multipartbody .= $content"rn";
  40.             } else {
  41. $multipartbody .= $MPboundary . "rn";
  42.                 $multipartbody .= 'content-disposition: form-data; name="' . $parameter . ""rnrn";
  43. $multipartbody .= $value."rn";
  44.             }
  45.         $multipartbody .= $endMPboundary;
  46. return $multipartbody;
  47.     }
  48.     function get_image($post_id){
  49. if( has_post_thumbnail($post_id) ){
  50.             $timthumb_src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id),'full');
  51. $output = $timthumb_src[0];
  52.         } else {
  53. $content = get_post_field('post_content', $post_id);
  54.             $defaltthubmnail = $this->default_image;
  55. "]?(.+?)['"]?(?:(?: | |
    |
    )+.*?)?>/sim', $content$strResult, PREG_PATTERN_ORDER);

  56.             $n = count($strResult[1]);
  57. if($n > 0){
  58.                 $output = $strResult[1][0];
  59. else {
  60.                 $output = $defaltthubmnail;
  61.         }
  62. return $output;
  63.     }
  64.     function new_post_photo($post) {
  65. global $post;
  66.         if$post->post_status != "publish" ){
  67. $token = $this->access_token;
  68.             $url = $this->host ."statuses/upload.json";
  69. $status = "我剛剛釋出了新文章 《".get_the_title()."》 。".get_permalink();
  70.             $status .= mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0,180,"..."); 
  71. $pic_path = self::get_image($post->ID);
  72.             $params = array();
  73. $params['access_token'] = $token;
  74.             $params['status'] = $status;
  75. $params['pic'] = '@'.$pic_path;
  76.             $body = self::build_http_query_multi($params);
  77. $result = self::do_mu_post($url,$body);
  78.     }
  79. $HMT = new sync_sina();

以上程式碼新增至主題 functions.php 檔案即可。

注意:

需要伺服器支援 file_get_contents 函式以及 curl 元件;

如果網站在國外伺服器或者服務因特殊原因遮蔽了 api.weibo.com 域名,則可能造成釋出文章卡死或者超時的問題。

如果網站未透過新浪稽核微博小尾巴會顯示為未稽核應用。

文章參考:http://www.mywpku.com/WordPress-sync-sina-weibo.html