// 微博同步
function post_to_sina_weibo($post_ID) {
if( wp_is_post_revision($post_ID) ) return;
$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' && $_POST['original_post_status'] != 'publish' )
{
$request = new WP_Http;
$status = '【' . strip_tags( $get_post_title ) . '】 ' . mb_strimwidth(strip_tags( apply_filters('the_content', $get_post_centent)),0, 132,'...') . ' 全文地址:' . get_permalink($post_ID) ;
$api_url = 'https://api.weibo.com/2/statuses/update.json';
$body = array( 'status' => $status, 'source'=>'xxxxxxxxxxx');
//你的新浪微博登陆名
// $username = '' ;
//你的新浪微博登陆密码
// $password = '' ;
// $headers = array( 'Authorization' => 'Basic ' . base64_encode('$username:$password'));
$headers = array( 'Authorization' => 'Basic ' . 'xxxxxx' );
/*
如果你使用改方法,请注释掉上面$headers = array( 'Authorization' => 'Basic ' . '1fFjYc3uQHZpcF32fS5jb146MxFeY19DYF53aWfzNA==' );
换成如下代码
//你的新浪微博登陆名
$username = '' ;
//你的新浪微博登陆密码
$password = '' ;
$headers = array( 'Authorization' => 'Basic ' . base64_encode('$username:$password'));
*/
$result = $request->post( $api_url , array( 'body' => $body, 'headers' => $headers ) );
}
}
add_action('publish_post', 'post_to_sina_weibo', 0);