微博发不出去显示服务器同步,WordPress发布文章同步到新浪微博失败的问题解决与分享...

张戈博客很久之前分享过一篇 WordPress发布文章同步到新浪微博 的文章,但经常有站长留言反馈同步失败,我一直觉得是代码部署问题。

最近很长一段时间,张戈博客也无法同步,我又觉得是微博自身的问题。直到近期抽空DeBUG了一下微博同步,取得了返回结果,才发现是由于网站IP变更导致的!

一、网站IP变更

如下是我DeBUG取得的返回 json 结果:

d33b3ad634b9fa0944ca99323fad87ee.png

格式化如下:

{

"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:

5add92ee43d6b51a387c98f2cac71b6e.png

所以,如果发现你部署了代码,却总是无法同步,请前往微博开放平台,如图查看并更正一下网站现用服务器的IP地址。

当然,修改后会进入二次审核状态,耐心等待好了。8bc698598d0fe15906c4dcaaa3d1dfed.png

Ps:其实最后我发现这里可以不填写任何IP,免得下次网站更换服务器又忘记修改了!反正大部分人也不怕自己的微博被盗用。

二、DeBUG 方法

如果,上述分享还不能解决你的问题,最后张戈再分享本文用到的DeBUG方法。

原理很简单,所谓的DeBUG就是取得微博同步的返回值,看下到底是什么原因不能同步。

DeBUG代码如下:

/**

* WordPress发布文章同步到新浪微博(DeBUG测试)

* 文章地址:https://zhang.ge/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

{

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('||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://zhang.ge/logo.png';

甚至,直接试了下图片的本地物理路径也不行。。。

最终,发现把https改成http就好了!!!原来不支持https图片?

那我把 $url 中的https强行替换成http就好了:

$url = preg_replace('/https:\/\//i','http://',$url);

果然,替换后就成功了:

a0d67235f11d8ff26142e666aecf390d.png81a17f0733c5dca9167d492617ff6baa.png

如果还是不行,请留言联系张戈!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值