< ?php
//
//twitter2renren.php
//By @yegle, yegle.net
//Licensed under CC by-nc-sa
//
$file = '用于记录你最后一次同步tweet id的文件路径。注意不要删除并让脚本能读取!';
$cookie_file = '保留curl模拟登陆renren.com的cookie文件路径。';
$twitter_username = '你的twitter用户名,注意是用户名,不是邮箱名';
$twitter_pwd = '你的twitter密码';
$hashtag = '如果你希望在你的推里包含特殊字符串时才转过去,请设置该变量,否则只转发不是以@开头的推';
$renren_username = 'renren.com登录用户名';
$renren_password = 'renren.com登录密码';
//配置完毕,请停止编辑文件!
if(file_exists($file)){
$lastid = intval(file_get_contents($file));
}
else{
touch($file);
$lastid = 0;
}
$timeline_url = 'http://'.$twitter_username.':'.$twitter_pwd.'@twitter.com/statuses/user_timeline.rss?count=200';
if($lastid!==0) $timeline_url.='&since_id='.$lastid;
$timeline = file_get_contents($timeline_url);
$obj = simplexml_load_string($timeline);
$items = $obj->channel->item;
if(empty($items)){
echo "no newer tweet\n";
exit();
}
if(sizeof($items)==1){
$new_id = preg_replace('/.*\/([0-9]*)$/','$1',strval($items->link));
}
else{
$new_id = preg_replace('/.*\/([0-9]*)$/','$1',strval($items[0]->link));
}
if($new_id == ''){
echo "error\n";
exit();
}
file_put_contents($file,$new_id);
$post_arr = array();
foreach($items as $tweet){
$text = substr($tweet->title,strlen($twitter_username)+2);
if(strpos($text,'@')!==0 && ( empty($hashtag) || strpos($text,$hashtag)!==FALSE)){
$text = str_replace("RT: @","转自",$text);
$post_arr[] = $text.' [twitter]';
}
}
if(empty($post_arr)){
echo "no need.\n";
exit();
}
//$post_arr = array_reverse($post_arr);
$renren_login = "http://passport.renren.com/PLogin.do";
$ch = curl_init();
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);
curl_setopt($ch,CURLOPT_URL,$renren_login);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,'email='.$renren_username.'&password='.$renren_password.'&autoLogin=true&origURL=http://www.renren.com/Home.do&domain=renren.com');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
#curl_setopt($ch,CURLOPT_VERBOSE,TRUE);
$str = curl_exec($ch);
$pattern = "/get_check:'([^']*)'/";
preg_match($pattern,$str,$matches);
$get_check = $matches[1];
curl_close($ch);
$renren_status = 'http://status.renren.com/doing/update.do';
foreach($post_arr as $item){
$post = 'c='.urlencode($item).'&raw='.urlencode($item).'&isAtHome=0&publisher_form_ticket='.$get_check;
$ch = curl_init($renren_status);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
curl_setopt($ch,CURLOPT_REFERER,'http://status.renren.com/ajaxproxy.htm');
$ret = curl_exec($ch);
curl_close($ch);
$ret = json_decode($ret,TRUE);
echo $ret['msg']."\n";
}
?>
近期评论