Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124

网站应用审核通过让我对新浪微博又提升了一些兴趣,比如这个发布新文章的时候同步到新浪微博,当然可以选择关联博客那种方式,但显然是这种方式更赞啊,因为还显示小尾巴(如果应用通过审核的话)
网站应用审核通过让我对新浪微博又提升了一些兴趣,比如这个发布新文章的时候同步到新浪微博,当然可以选择关联博客那种方式,但显然是这种方式更赞啊,因为还显示小尾巴(如果应用通过审核的话)。
为什么要同步到微博
想发布微博就要获取access_token,有两种方法,如果你有通过审核的应用什么都好说,如果没有的话,可以用我以前获取微博的方法得到access_token,直接填写到代码里,效果都是一样的。
直接上代码,不废话,直接放到funtions.php中即可
由于发微博只能用POST方式,所以我们还要模拟POST。
add_action('publish_post', 'new_post_weibo');
function new_post_weibo($post_ID) {
global $wpdb;
$this_user = wp_get_current_user();
$token = get_user_meta($this_user->ID,"sina_access_token",true);//如果直接获取的access_token就直接诶写到这里
if($token && !wp_is_post_revision($post_ID)){
$url = "https://api.weibo.com/2/statuses/update.json";
$status = "我刚刚发布了新文章《".get_the_title($post_ID)."》。".get_permalink($post_ID);
$data = "access_token=" . $token . "&status=" . urlencode ($status);
$output = json_decode(do_post($url,$data));
update_post_meta($post_ID,'weibo_id', number_format($output['id'],0,'',''));
}
}
//curl模拟POST
function do_post($url, $data) {
$ch = curl_init ();
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;
}
PS:微博登录本站评论会在评论中显示个人微博地址,快来加粉吧。
曾经不太理解授权机制的时候还需要下载SDK,现在发现居然如此简单,我真是个傻逼。
原文地址 http://fatesinger.com/529