豆瓣/人人/BBS/开心/嘀咕/网易微博/新浪微博状态更新PHP代码终结版

2010-08-21 9:46 by hackerzhou

最初是为了把博客自动同步到泉和人人所以研究了下php的curl,之后不断的更新支持的系统,昨天决定不再在这个东西上浪费时间了,毕竟重复的东西太多了,写多了没意思。于是就修复了些bug,增加了新浪微博/网易微博的支持后就不再开发了,接下去几天想把原来做过的一些小工具重构代码修复bug后出新版本。

之前写过的几篇:
开心网&豆瓣网发布状态PHP脚本(附Ping.fm的Custom URL使用方法)

PHP的日月光华/复旦泉BBS发文脚本
php下用curl做的一个更新人人网状态的代码

这次使用了页面返回值来判定是否状态发布成功,如果不成功的话最多会重试三次,尽量减少网络问题造成的影响。使用Ping.fm的CUSTOM URL功能的朋友可以修改一下直接放到自己的php空间中,在Ping.fm里面设置下Custom URL的地址就可以了。文中以及代码中的BBS对应的是复旦大学的日月光华BBS以及复旦泉BBS,不适用于别的学校。

感谢在博客评论的tang提醒了我开心网能够用wap页面来发状态,登录比html页面的方便许多,而且之前写的那个模拟http://s.kaixin001.com.cn/js/enlogin-7.js的加密函数在windows下跑结果是对的,在linux上跑居然结果不对,相当诡异,懒得去研究了,故索性使用wap页面登录&提交状态。要启用对应的服务,把enable_xxx修改为true,填写对应的登录用户名/密码即可。

比如:

	"enable_douban"=>"true",
	"douban_username"=>"[email protected]",
	"douban_password"=>"123456",

终结版代码如下(点击展开):

<?php
$settings;
init($settings);
//Ping.fm的Custom URL通过POST传method,title,message过来
$method=$_POST["method"];
$title=$_POST["title"];
$message=$_POST["message"];
if($message!=""){
	send($method,$title,$message,$settings);
}

function init(&$settings){
	//要启用某个服务,请将对应的enable_xxx项设置为true,默认为false不启动
	$settings=array(
		"enable_douban"=>"false",
		"douban_username"=>"##豆瓣用户名",
		"douban_password"=>"##豆瓣密码",

		"enable_renren"=>"false",
		"renren_username"=>"##人人网用户名",
		"renren_password"=>"##人人网密码",

		"enable_bbs"=>"false",
		"bbs_username"=>"##BBS用户名",
		"bbs_password"=>"##BBS密码",
		"bbs_board_id"=>"##BBS版面ID",
		"bbs_night_api"=>"##晚间访问BBS的API",
		"bbs_host"=>"##BBS地址",

		"enable_kaixin001"=>"false",
		"kaixin001_username"=>"##开心网用户名",
		"kaixin001_password"=>"##开心网密码",

		"enable_digu"=>"false",
		"digu_username"=>"##嘀咕用户名",
		"digu_password"=>"##嘀咕密码",

		"enable_netease"=>"false",
		"netease_username"=>"##网易微博用户名",
		"netease_password"=>"##网易微博密码",

		"enable_sina"=>"false",
		"sina_username"=>"##新浪微博用户名",
		"sina_password"=>"##新浪微博密码"
	);
}

function send($method,$title,$message,&$settings){
	if($settings["enable_renren"]==="true"){
		updateRenren($message,$settings["renren_username"],$settings["renren_password"],0);
	}
	if($settings["enable_sina"]==="true"){
		updateSina($message,$settings["sina_username"],$settings["sina_password"],0);
	}
	if($settings["enable_kaixin001"]==="true"){
		updateKaixin001($message,$settings["kaixin001_username"],$settings["kaixin001_password"],0);
	}
	if($settings["enable_digu"]==="true"){
		updateDigu($message,$settings["digu_username"],$settings["digu_password"],0);
	}
	if($settings["enable_netease"]==="true"){
		updateNetEase($message,$settings["netease_username"],$settings["netease_password"],0);
	}
	if($settings["enable_douban"]==="true"){
		updateDouban($message,$settings["douban_username"],$settings["douban_password"],0);
	}
	if($settings["enable_bbs"]==="true"){
		if($method=="status"||$title==""){
			$title="[Ping.fm状态]";
		}
		updateBBS($title,$message,$settings["bbs_board_id"],$settings["bbs_username"],$settings["bbs_password"],$settings["bbs_host"],$settings["bbs_night_api"],0);
	}
}

function updateSina($status,$sina_username,$sina_password,$tried_times){
	if($tried_times>=3){
		return false;
	}
	$cookie_jar=tempnam('./tmp','sina');
	$ch=&getCurl($cookie_jar,"POST","http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.9)");
	curl_setopt($ch,CURLOPT_POSTFIELDS,"service=miniblog&client=ssologin.js%28v1.3.9%29&entry=miniblog&encoding=utf-8&gateway=1&savestate=7&from=&useticket=0&username=".urlencode($sina_username)."&password=".urlencode($sina_password)."&url=http%3A%2F%2Ft.sina.com.cn%2Fajaxlogin.php%3Fframelogin%3D1%26callback%3Dparent.sinaSSOController.feedBackUrlCallBack&returntype=META");
	$str=&sendAndResult($ch);
	$ch=&getCurl($cookie_jar,"POST","http://t.sina.com.cn/mblog/publish.php?rnd=0.10724102530252999");
	curl_setopt($ch,CURLOPT_POSTFIELDS,"content=".urlencode($status)."&pic=&styleid=1&retcode=&imgType=");
	curl_setopt($ch,CURLOPT_REFERER,"http://t.sina.com.cn/hackerzhou");
	$str=&sendAndResult($ch);
  if(strstr($str,"{\"code\":\"A00006\",")!==false){
		echo "[Sina] succeed\n";
	}else{
		echo "[Sina] failed\n";
		updateSina($status,$sina_username,$sina_password,$tried_times+1);
	}
}

function updateNetEase($status,$netease_username,$netease_password,$tried_times){
	if($tried_times>=3){
		return false;
	}
	$cookie_jar=tempnam('./tmp','163');
	$ch=&getCurl($cookie_jar,"POST","http://reg.163.com/logins.jsp");
	curl_setopt($ch,CURLOPT_POSTFIELDS,'username='.urlencode($netease_username).'&password='.urlencode($netease_password).'&savelogin=1&url=http://t.163.com/session/first&product=t&type=1');
	$str=&sendAndResult($ch);
	$ch=&getCurl($cookie_jar,"POST","http://t.163.com/statuses/update.do");
	curl_setopt($ch,CURLOPT_POSTFIELDS,'status='.urlencode($status).'&in_reply_to_status_id=sendinfo');
	$ret=&sendAndResult($ch);
  if(strstr($ret,"created_at")!==false){
		echo "[t.163.com] succeed\n";
	}else{
		echo "[t.163.com] failed\n";
		updateNetEase($status,$netease_username,$netease_password,$tried_times+1);
	}
}

function updateBBS($title,$content,$boardId,$bbs_username,$bbs_password,$bbs_host,$night_api,$tried_times) {
	if($tried_times>=3){
		return false;
	}
	$newtitle=iconv("UTF-8","GB2312",$title);
	$newcontent=iconv("UTF-8","GB2312",$content);
	$hour=intval(date("H"));
	if($hour==0||($hour>=16&&$hour<=23)){
		$bbs_host=$night_api;
	}
	$cookie_jar=tempnam('./tmp','bbs');
	$ch=&getCurl($cookie_jar,"POST",$bbs_host."/bbs/login");
	curl_setopt($ch,CURLOPT_POSTFIELDS,'id='.urlencode($bbs_username).'&pw='.urlencode($bbs_password));
	$str=&sendAndResult($ch);
	$ch=&getCurl($cookie_jar,"POST",$bbs_host."/bbs/snd?bid=".$boardId."&f=&e=0");
	curl_setopt($ch,CURLOPT_REFERER,$bbs_host.'/bbs/pst?bid='.$boardId);
	curl_setopt($ch,CURLOPT_POSTFIELDS,'title='.rawurlencode($newtitle).'&sig=3&text='.urlencode($newcontent));
	$str=&sendAndResult($ch);
	$str=iconv("GB2312","UTF-8",$str);
	$ch=&getCurl($cookie_jar,"GET",$bbs_host."/bbs/logout");
	curl_setopt($ch,CURLOPT_REFERER,$bbs_host.'/bbs/');
	$ret=&sendAndResult($ch);
	if(strstr($str,"成功")!==false){
		echo "[BBS] succeed\n";
	}else{
		echo "[BBS] failed\n";
		updateBBS($title,$content,$boardId,$bbs_username,$bbs_password,$bbs_host,$night_api,$tried_times+1);
	}
}

function updateRenren($status,$renren_username,$renren_password,$tried_times){
	if($tried_times>=3){
		return false;
	}
	$cookie_jar=tempnam('./tmp','renren');
	$ch=&getCurl($cookie_jar,"POST","http://passport.renren.com/PLogin.do");
	curl_setopt($ch,CURLOPT_POSTFIELDS,'email='.urlencode($renren_username).'&password='.urlencode($renren_password).'&autoLogin=true&origURL=http%3A%2F%2Fwww.renren.com%2FHome.do&domain=renren.com');
	$str=&sendAndResult($ch);
	$pattern="/get_check:'([^']+)'/";
	preg_match($pattern,$str,$matches);
	$get_check=$matches[1];
	$ch=&getCurl($cookie_jar,"POST","http://status.renren.com/doing/update.do");
	curl_setopt($ch,CURLOPT_POSTFIELDS,'c='.urlencode($status).'&raw='.urlencode($status).'&isAtHome=1&publisher_form_ticket='.$get_check.'&requestToken='.$get_check);
	curl_setopt($ch,CURLOPT_REFERER,'http://status.renren.com/ajaxproxy.htm');
	$ret=&sendAndResult($ch);
  if(strpos($ret,"{\"allMsg\":")===0){
		echo "[Renren] succeed\n";
	}else{
		echo "[Renren] failed\n";
		updateRenren($status,$renren_username,$renren_password,$tried_times+1);
	}
}

function updateKaixin001($status,$kaixin001_username,$kaixin001_password,$tried_times){
	if($tried_times>=3){
		return false;
	}
	$cookie_jar=tempnam('./tmp','kaixin001');
	$ch=&getCurl($cookie_jar,"POST","http://wap.kaixin001.com/home/?id=");
	curl_setopt($ch,CURLOPT_POSTFIELDS,'email='.$kaixin001_username.'&password='.$kaixin001_password.'&remember=1&from=&refuid=0&refcode=&bind=&gotourl=&login=+%E7%99%BB+%E5%BD%95+');
	$str=&sendAndResult($ch);
	$pattern="/state.php\?verify=([^\"]+)\"/";
	preg_match($pattern,$str,$matches);
	$verify=$matches[1];
	$ch=&getCurl($cookie_jar,"POST","http://wap.kaixin001.com/home/state_submit.php?verify=".$verify);
	curl_setopt($ch,CURLOPT_POSTFIELDS,'state='.urlencode($status));
	curl_setopt($ch,CURLOPT_REFERER,'	http://wap.kaixin001.com/home/');
	$ret=&sendAndResult($ch);
	if(strstr($ret,"成功")!==false){
		echo "[Kaixin001] succeed\n";
	}else{
		echo "[Kaixin001] failed\n";
		updateKaixin001($status,$kaixin001_username,$kaixin001_password,$tried_times+1);
	}
}

function updateDouban($status,$douban_username,$douban_password,$tried_times){
	if($tried_times>=3){
		return false;
	}
	$cookie_jar=tempnam('./tmp','douban');
	$ch=&getCurl($cookie_jar,"POST","http://www.douban.com/accounts/login");
	curl_setopt($ch,CURLOPT_POSTFIELDS,'form_email='.urlencode($douban_username).'&form_password='.urlencode($douban_password).'&remember=on&user_login=%E8%BF%9B%E5%85%A5');
	$str=&sendAndResult($ch);
	$pattern="/<input type=\"hidden\" name=\"ck\" value=\"([^\"]+)\"/";
	preg_match($pattern,$str,$matches);
	$ck=$matches[1];
	$ch=&getCurl($cookie_jar,"POST","http://www.douban.com/");
	curl_setopt($ch,CURLOPT_POSTFIELDS,'ck='.$ck.'&mb_text='.urlencode($status));
	curl_setopt($ch,CURLOPT_REFERER,'http://www.douban.com/');
	$ret=&sendAndResult($ch);
	if(strstr(strip_tags($ret),strip_tags($status))!==false){
		echo "[Douban] succeed\n";
	}else{
		echo "[Douban] failed\n";
		updateDouban($status,$douban_username,$douban_password,$tried_times+1);
	}
}

function updateDigu($status,$digu_username,$digu_password,$tried_times){
	if($tried_times>=3){
		return false;
	}
	$cookie_jar=tempnam('./tmp','digu');
	$ch=&getCurl($cookie_jar,"POST","http://digu.com/login");
	curl_setopt($ch,CURLOPT_POSTFIELDS,'remember=1&userName='.urlencode($digu_username).'&password='.$digu_password.'&action=1');
	$str=&sendAndResult($ch);
	$ch=&getCurl($cookie_jar,"POST","http://digu.com/login");
	curl_setopt($ch,CURLOPT_POSTFIELDS,'remember=1&userName='.urlencode($digu_username).'&password='.$digu_password.'&action=1');
	$str=&sendAndResult($ch);
	$pattern="/',id:'([^']+)/";
	preg_match($pattern,$str,$matches);
	$cookiesId=$matches[1];
	$ch=&getCurl($cookie_jar,"POST","http://digu.com/jump?aid=addLekuData");
	curl_setopt($ch,CURLOPT_POSTFIELDS,"type=1&text=".urlencode($status)."&tw_userId=undefined&tId=0&cookieId=".$cookiesId."&id=0&groupId=&ladygg=undefined&songId=0&songTitle=&imageName=&addressId=0");
	$ret=&sendAndResult($ch);
	if(strstr($ret,"success")!==false){
		echo "[Digu] succeed\n";
	}else{
		echo "[Digu] failed\n";
		updateDigu($status,$digu_username,$digu_password,$tried_times+1);
	}
}

function &getCurl($jar,$method,$url){
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_COOKIEJAR,$jar);
	curl_setopt($ch,CURLOPT_COOKIEFILE,$jar);
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
	curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
	//curl_setopt($ch,CURLOPT_VERBOSE,true);
	if($method=="POST"){
		curl_setopt($ch,CURLOPT_POST,TRUE);
	}else{
		curl_setopt($ch,CURLOPT_POST,FALSE);
	}
	return $ch;
}

function &sendAndResult(&$ch){
	$str=curl_exec($ch);
	curl_close($ch);
	unset($ch);
	return $str;
}
?>
本文基于 署名 2.5 中国大陆 许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名 hackerzhou 并包含 原文链接
发表评论

本文有 6 条评论

  1. 你写的文章的技术性都很高啊
    2011-04-22 21:23

    以后还来看你写的文章。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

    • hackerzhou
      2011-04-23 09:13

      这。。。其实还好,写一些大家都能理解的东西,有些晦涩的就不怎么写了

发表评论