微博同步代码片段

作者: shaneZhang 分类: 互联网技术 发布时间: 2014-12-30 23:37

// 微博同步 
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);

本页面支持繁体中文友好显示:微博同步代码片段

如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复