常常上微博而且关心了许多官方认证号的朋友们一定会发觉许多官方网特性的新浪微博都是会把网址的热门文章或是动态性同歩到微博这样一来就可以让微博用户迅速 的认识到站点的最新资讯,而且也为网址添加了一个曝出及其总流量通道,针对升级多次的网址而言提升一个同歩网址动态性到新浪微博的功用是十分需要的,都不胡扯 了,立即上编码:
- 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 . “rn”;
- $multipartbody .= ‘Content-Disposition: form-data; name=“‘ . $parameter . ‘”; filename=“‘ . $filename . ‘”‘. “rn”;
- $multipartbody .= “Content-Type: image/unknownrnrn”;
- $multipartbody .= $content. “rn”;
- } else {
- $multipartbody .= $MPboundary . “rn”;
- $multipartbody .= ‘content-disposition: form-data; name=“‘ . $parameter . ““rnrn”;
- $multipartbody .= $value.“rn”;
- }
- }
- $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域名,则很有可能导致公布文章内容卡住或是中断的问题。
假如网址未根据新浪网审批微博小尾巴会展示为未审核运用。
文章内容参照:http://www.mywpku.com/wordpress-sync-sina-weibo.html