wordpress发布文章时同步到新浪微博
在百度进行搜索文章时,发现不少的博主文章都是有好几条的搜索结果,原来除了自己的官网博客外,还将文章同步到了新浪微博中,怪不得有怎么多条搜索的结果呢,这样做的最大好处就是让微博给自己的站点进行了曝光,尤其对于新站点来说,这样的功能就更不能缺少了,因为新站发布的文章百度都要过一段时间才能收录,因为没有百度蜘蛛过来抓取,但百度蜘蛛对于新浪微博可就不同了,新浪微博的权重很高,所以导致百度的抓取是最频繁的,所以一旦我们将文章发布到新浪微博上面,就会吸引来蜘蛛,然后会顺着新浪微博来到自己的博客中进行收录,哪么下面夏日博客就来教大家如何将 wordpress 的文章同步发送到新浪微博中,代码如下:
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 86 87 88 89 90 91 92 93 94 95 96 97 | class sync_sina { public $access_token = "" ; //access_token public $default_image = "" ; //默认图片地址 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 组件,缺一不可额。