Re: php curl函数模拟浏览器抓取网站信息
所属分类:PHP工具与代码
  摘几个从别的网站扣过来的例子: PHP代码
[code]$cookie_jar = tempnam('./tmp','cookie');
$ch = curl_init(); curl_setopt($ch,CURLOPT_URL, 'http://******');
curl_setopt($ch, CURLOPT_POST, 1);
$request = 'email_address=&password=&action=';
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); //把返回来的cookie信息保存在$cookie_jar文件中
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); //设定返回的数据是否自动显示
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设定是否显示头信息
curl_setopt($ch, CURLOPT_HEADER, false); //设定是否输出页面内容
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_exec($ch);
curl_close($ch); //get data after login[/code]

PHP代码
[code]$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, 'http://*****');
curl_setopt($ch2, CURLOPT_HEADER, false);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar);
$orders = curl_exec($ch2);
echo '';

echo strip_tags($orders);
echo '';
curl_close($ch2); 实践证明很稳定:)

再把我今天写的贴上来:

PHP代码
set_time_limit(0);
function _rand() {
$length=26;
$chars = "0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
$string = '';
for($i = 0; $i < $length; $i++) {
$string .= $chars[mt_rand(0, $max)];
}
return $string;
}
$HTTP_SESSION=_rand();
echo $HTTP_SESSION;
$HTTP_Server="www.baidu.com";
$HTTP_URL="/";
$ch = curl_init();
curl_setopt ($ch,CURLOPT_URL,"http://".$HTTP_Server.$HTTP_URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
//curl_setopt($ch,CURLOPT_COOKIE,$HTTP_SESSION);
$res = curl_exec($ch);
curl_close ($ch);
print_r($res);[/code]