本文實例講述了php采用curl模仿登錄人人網發布動態的方法。分享給大家供大家參考。具體實現方法如下:
說到php中模仿登錄很多人第一時間會想到curl函數系列了,這個沒錯本例子也是使用curl模仿登錄之後再進行動態發布,原理也簡單我們只要抓取人人網的登錄信息,然後再由curl post登錄數據上去就可以了。
具體代碼如下:
復制代碼 代碼如下:$rconfig = pdo_fetch("SELECT * FROM ".tablename("eduTwo_renren")." WHERE weid = :weid",array(':weid'=>$_W['weid']));
$cookie_file = dirname(__FILE__)."/renren.cookie";
$login_url = 'http://passport.renren.com/PLogin.do';
$post_fields['email'] = $rconfig['rusername'];
$post_fields['password'] = $rconfig['rpassword'];
$post_fields['origURL'] = 'http%3A%2F%2Fhome.renren.com%2FHome.do';
$post_fields['domain'] = 'renren.com';
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
//var_dump($info);exit;
//匹配用戶的ID
$send_url='http://www.renren.com/home';
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
//$uid = "305115027";
//獲取token和rtk
$send_url=$info['redirect_url'];
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$tmp = curl_exec($ch);
curl_close($ch);
preg_match_all("/get_check:'(.*?)',get_check_x:'(.*?)',/is",$tmp,$arr);
preg_match_all("/'ruid':'(.*?)',/is",$tmp,$utmp);
//var_dump($utmp);exit;
$token = $arr[1][0];//1121558104
$rtk = $arr[2][0];//e9a9cb2
$uid = $utmp[1][0];
//echo $token;exit;
//發布信息
$poststr['content'] = $_GPC['content'].$rconfig['tail'];
$poststr['withInfo'] = '{"wpath":[]}';
$poststr['hostid:'] = $uid;
$poststr['privacyParams'] = '{"sourceControl": 99}';
$poststr['requestToken'] = $token;
$poststr['_rtk'] = $rtk;
$poststr['channel'] = "renren";
$head = array(
'Referer:http://shell.renren.com/ajaxproxy.htm',
'X-Requested-With:XMLHttpRequest',
);
$ch = curl_init("http://shell.renren.com/{$uid}/status");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
curl_setopt($ch,CURLOPT_HTTPHEADER,$head);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $poststr);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$content = curl_exec($ch);
curl_close($ch);
//echo $content;exit;
$data = json_decode($content,true);
if($data["code"] == "0"){
echo "發布成功!";
}else{
echo "shit !!!";
}
最後就發布成功了,當然前面的數據庫需要自己寫一個吧,非常的簡單的一個記錄庫也是你要發布的信息。錄數據上去就可以了。
希望本文所述對大家的PHP程序設計有所幫助。
你可以看下是否有其它的http header沒有模擬,比如Referer和User-Agent是否都能模擬浏覽器的值,一個完整的請求是類似於這樣的:
GET /home/pack/data/content?id=31,2399,13,30&asyn=1&t=0.03439752989200834&_req_seqid=0xa982225f0637c78a HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Referer: www.baidu.com/
x-requested-with: XMLHttpRequest
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS123401; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; MS-RTC LM 8)
Host: www.baidu.com
Connection: Keep-Alive
Cookie: XCXXXXX
$post_fields = 'cktime=31536000&step=2&pwuser=賬號&pwpwd=密碼';賬號密碼改成你自己的試試,我試過是可以的