phpcurl_setopt登入獲取資料

風來了發表於2013-03-06

登入 :以51啦統計資料為例

$cookfile = dirname(__FILE__).`/cookie.txt`;
$ip=$_SERVER[`SERVER_ADDR`];
$uname="帳號";
$upass="密碼";
// 設定URL和相應的選項
$HTTP_REQUEST_HEADER = array(
		"method"	 => 	 "POST",
		"timeout"	 =>	 30,
		"Content-Type"	=>	 "application/x-www-form-urlencoded; charset=gb2312",
		"Referer"	 =>	 "http://www.51.la/user/notice.asp",
		"Client-IP"	 =>	 $ip,
		"X-Forwarded-For"	=>	$ip,
		"Host"	 =>	 "http://www.51.la/"
);

$url = "http://www.51.la/login.asp";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //這行是設定curl是否跟隨header傳送的location,重要
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[`HTTP_USER_AGENT`]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $HTTP_REQUEST_HEADER);
curl_setopt($ch, CURLOPT_POSTFIELDS, "uname=$uname&upass=$upass");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookfile);   // 儲存cookie
//curl_setopt($ch, CURLOPT_COOKIEFILE, $cookfile);	// 傳送 cookie
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
//以下為SSL設定
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);

$res = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
if($res===false){
exit(`false`);
}elseif(strpos($res,$uname)>0){//查詢使用者名稱是否存在,存在則登入正確
echo `登入成功`;
}else{
echo $res;
}

獲取統計資訊:

$cookfile = dirname(__FILE__).`/cookie.txt`;
$ip=$_SERVER[`SERVER_ADDR`];
// 設定URL和相應的選項
$HTTP_REQUEST_HEADER = array(
		"method"	 => 	 "POST",
		"timeout"	 =>	 30,
		"Content-Type"	=>	 "application/x-www-form-urlencoded; charset=gb2312",
		"Referer"	 =>	 "http://www.51.la/user/notice.asp",
		"Client-IP"	 =>	 $ip,
		"X-Forwarded-For"	=>	$ip,
		"Host"	 =>	 "http://www.51.la/"
);
$url = "http://www.51.la/report/1_main.asp?id=2157050";
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //這行是設定curl是否跟隨header傳送的location,重要
curl_setopt($ch1, CURLOPT_COOKIEFILE, $cookfile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookfile);   // 儲存cookie
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
$res = curl_exec($ch1);
//關閉cURL資源,並且釋放系統資源
curl_close($ch1);
if($html===false){
	exit(`false`);
}
$html = str_replace(array("
", "
"), ``, $html);
$html=cut_html($html,`<!--Ajiang Stat 2.0--><img`,`</div><div style="width:550px;`);
$html=iconv("GB2312//IGNORE","UTF-8",$html);
$html=str_replace(array(` `,`&nbsp;`),``,$html);
preg_match_all(`/>(.*)IP/i`, $html, $a);
$ip=$a[1];
preg_match_all(`/IP/(.*)UV//i`, $html, $a);
$uv=$a[1];
preg_match_all(`/pv"/>(.*)PV/i`, $html, $a);
$pv=$a[1];


相關文章