WordPress釋出文章同步到新浪微博失敗的問題解決與分享
張戈部落格很久之前分享過一篇 WordPress 釋出文章同步到新浪微博 的文章,但經常有站長留言反饋同步失敗,我一直覺得是程式碼部署問題。
最近很長一段時間,張戈部落格也無法同步,我又覺得是微博自身的問題。直到近期抽空 DeBUG 了一下微博同步,取得了返回結果,才發現是由於網站 IP 變更導致的!
一、網站 IP 變更
如下是我 DeBUG 取得的返回 json 結果:
格式化如下:
{ "headers": { "server": "nginx/1.6.1", "date": "Sat, 16 Jan 2016 11:43:34 GMT", "content-type": "application/json;charset=UTF-8", "connection": "close", "api-server-ip": "10.75.5.90", "vary": "Accept-Encoding" }, "body": "{"error":"Ip Limit, request ip is not contained in safety ip","error_code":10004,"request":"/2/statuses/upload_url_text.json"}", "response": { "code": 400, "message": "Bad Request" }, "cookies": [ ], "filename": null }
其中“Ip Limit, request ip is not contained in safety ip”很明顯的指出了錯誤原因:當前 IP 不在微博伺服器白名單列表當中,說白了就是網站換了 IP 地址,和你之前申請微博應用時填寫的 IP 不一致了!
前往新浪微博開放平臺看了下,裡面設定的 IP 果然還是之前的老 IP:
所以,如果發現你部署了程式碼,卻總是無法同步,請前往微博開放平臺,如圖檢視並更正一下網站現用伺服器的 IP 地址。
當然,修改後會進入二次稽核狀態,耐心等待好了。
Ps:其實最後我發現這裡可以不填寫任何 IP,免得下次網站更換伺服器又忘記修改了!反正大部分人也不怕自己的微博被盜用。
二、DeBUG 方法
如果,上述分享還不能解決你的問題,最後張戈再分享本文用到的 DeBUG 方法。
原理很簡單,所謂的 DeBUG 就是取得微博同步的返回值,看下到底是什麼原因不能同步。
DeBUG 程式碼如下:
<?php /** * WordPress釋出文章同步到新浪微博(DeBUG測試) * 文章地址:https://zhangge.net/5082.html */ ini_set('display_errors', true); require('./wp-blog-header.php'); header("Content-type: text/html;charset=UTF-8"); header('HTTP/1.1 200 OK'); function post_to_sina_weibo_test($post_ID) { $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') { $appkey='1034947262'; /* 此處是你的新浪微博appkey,不修改的話就會顯示來自張戈部落格哦! */ $username='微博使用者名稱'; $userpassword='微博密碼'; $request = new WP_Http; $keywords = ""; /* 獲取文章標籤關鍵詞 */ $tags = wp_get_post_tags($post_ID); foreach ($tags as $tag ) { $keywords = $keywords.'#'.$tag->name."#"; } /* 修改了下風格,並新增文章關鍵詞作為微博話題,提高與其他相關微博的關聯率 */ $string1 = '【文章釋出】' . strip_tags( $get_post_title ).':'; $string2 = $keywords.' 檢視全文:'.get_permalink($post_ID); /* 微博字數控制,避免超標同步失敗 */ $wb_num = (138 - WeiboLength_test($string1.$string2))*2; $status = $string1.mb_strimwidth(strip_tags( apply_filters('the_content', $get_post_centent)),0, $wb_num,'...').$string2; /* 獲取特色圖片,如果沒設定就抓取文章第一張圖片 */ $url = get_mypost_thumbnail($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)); return json_encode($result); } } /* //獲取微博字元長度函式 */ function WeiboLength_test($str) { $arr = arr_split_zh_test($str); //先將字串分割到陣列中 foreach ($arr as $v){ $temp = ord($v); //轉換為ASCII碼 if ($temp > 0 && $temp < 127) { $len = $len+0.5; }else{ $len ++; } } return ceil($len); //加一取整 } /* //拆分字串函式,只支援 gb2312編碼 //參考:http://u-czh.iteye.com/blog/1565858 */ function arr_split_zh_test($tempaddtext){ $tempaddtext = iconv("UTF-8", "GBK//IGNORE", $tempaddtext); $cind = 0; $arr_cont=array(); for($i=0;$i<strlen($tempaddtext);$i++) { if(strlen(substr($tempaddtext,$cind,1)) > 0){ if(ord(substr($tempaddtext,$cind,1)) < 0xA1 ){ //如果為英文則取1個位元組 array_push($arr_cont,substr($tempaddtext,$cind,1)); $cind++; }else{ array_push($arr_cont,substr($tempaddtext,$cind,2)); $cind+=2; } } } foreach ($arr_cont as &$row) { $row=iconv("gb2312","UTF-8",$row); } return $arr_cont; } /** * WordPress 獲取文章圖片加強版 By 張戈部落格 */ if(!function_exists('get_mypost_thumbnail')){ function get_mypost_thumbnail($post_ID){ if (has_post_thumbnail()) { $timthumb_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_ID), 'full' ); $url = $timthumb_src[0]; } else { if(!$post_content){ $post = get_post($post_ID); $post_content = $post->post_content; } preg_match_all('|<img.*?src=['"](.*?)['"].*?>|i', do_shortcode($post_content), $matches); if( $matches && isset($matches[1]) && isset($matches[1][0]) ){ $url = $matches[1][0]; }else{ $url = ''; } } return $url; } } echo post_to_sina_weibo_test(5082); //此處數字改成部落格已釋出文章的ID即可
先根據自己微博修改以上程式碼中的微博 app_key、使用者名稱、密碼。
然後,將最後一句程式碼中的 5082 改成你部落格已釋出文章的 ID。
最後,將程式碼儲存為 php 檔案(比如 test.php),上傳到網站根目錄並在瀏覽器訪問即可看到微博同步返回的結果了!
能看到失敗原因,相信問題已經解決了一半,是不是又學到了一招呢?
三、https 導致失敗
早上發現 IP 變更已經稽核通過了,就更新文章試了下,還是沒有同步成功!吶尼?
然後,又試了下 DeBUG 大法,發現返回如下:
{
"headers": {
"server": "nginx/1.6.1",
"date": "Tue, 19 Jan 2016 02:40:01 GMT",
"content-type": "application/json;charset=UTF-8",
"connection": "close",
"api-server-ip": "10.75.5.68",
"vary": "Accept-Encoding"
},
"body": "{"error":"does multipart has image?","error_code":20007,"request":"/2/statuses/upload_url_text.json"}",
"response": {
"code": 400,
"message": "Bad Request"
},
"cookies": [],
"filename": null
}
error:does multipart has image? 什麼鬼??
網上找了半天,基本都是說圖片不是本地的,或者上傳封裝得不對之類的。
於是,強行將 $url 這個變數指定為具體圖片地址,發現還是這個錯誤:
$url= 'https://zhangge.net/logo.png';
甚至,直接試了下圖片的本地物理路徑也不行。。。
最終,發現把 https 改成 http 就好了!!!原來不支援 https 圖片?
那我把 $url 中的 https 強行替換成 http 就好了:
$url = preg_replace('/https:///i','http://',$url);
果然,替換後就成功了:
如果還是不行,請留言聯絡張戈!