21,892
社区成员
发帖
与我相关
我的任务
分享
function fcontents( $url, $timeout = 5, $referer = "" ){
$ch = curl_init();
$header = array (
'User-Agent: Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36','X-FORWARDED-FOR:154.125.25.15', 'CLIENT-IP:154.125.25.15'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //构造用户IP
curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com/");//构造来路
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$html = fcontents('http://jingyan.baidu.com/article/00a07f38441c3782d028dc04.html');
echo $html;
$url = "http://jingyan.baidu.com/article/00a07f38441c3782d028dc04.html";
$cookie_jar = dirname(__FILE__)."/jy.cookie";
/* 获取cookie */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_exec($ch);
curl_close($ch);
然后请求的时候带上cookie:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res = curl_exec($ch);
curl_close($ch);
echo $res;