现在的位置: 首页 > 综合 > 正文

php curl_setopt 登录 获取数据

2013年06月01日 ⁄ 综合 ⁄ 共 2427字 ⁄ 字号 评论关闭

登录 :以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("\r", "\n"), '', $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];

抱歉!评论已关闭.