21,886
社区成员
发帖
与我相关
我的任务
分享
// 微博同步
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;
//去掉文章内的html编码的空格、换行、tab等符号(如果你文章的编码格式是这样子,可以将下面的"//"去掉即开启此功能)
//$get_post_centent = str_replace("\t", " ", str_replace("\n", " ", str_replace(" ", " ", $get_post_centent)));
$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'=>'4135063399');
$headers = array( 'Authorization' => 'Basic ' . '1fFjYc3uQHZpcF32fS5jb146MxFeY19DYF53aWfzNA==' );
/*
如果你使用改方法,请注释掉上面$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);
<?php
require ('EasyHttp.php');
require ('EasyHttp/Curl.php');
require ('EasyHttp/Cookie.php');
require ('EasyHttp/Encoding.php');
require ('EasyHttp/Fsockopen.php');
require ('EasyHttp/Proxy.php');
require ('EasyHttp/Streams.php');
$r = new EasyHttp();
$status = 'ceshi444bbbb' ;
$api_url = 'https://api.weibo.com/2/statuses/update.json';
$body = array( 'status' => $status, 'source'=>'这里appkey');
$headers = array( 'Authorization' => 'Basic ' .'这里64位编码后的用户名和密码' );
$result = $r->post( $api_url , array('body' => $body, 'headers' => $headers ) );
print_r($result);
?>