閱讀439 返回首頁    go 阿裏雲 go 技術社區[雲棲]


php curl_setopt 登錄 獲取數據

登錄 :以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"	 =>	 "https://www.51.la/user/notice.asp",
		"Client-IP"	 =>	 $ip,
		"X-Forwarded-For"	=>	$ip,
		"Host"	 =>	 "https://www.51.la/"
);

$url = "https://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"	 =>	 "https://www.51.la/user/notice.asp",
		"Client-IP"	 =>	 $ip,
		"X-Forwarded-For"	=>	$ip,
		"Host"	 =>	 "https://www.51.la/"
);
$url = "https://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("\r", "\n"), '', $html);
$html=cut_html($html,'<!--Ajiang Stat 2.0--><img','</div><div 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];



最後更新:2017-04-03 22:15:26

  上一篇:go 我國首份IP地理信息標準庫發布
  下一篇:go Oracle中登陸時報ORA-28000: the account is locked-的錯