// curl 伪造agent抓取页面
function http_get($URL,$key='baidu') {
$agent = Flight::get('flight.spider_agent')[$key];
if( !$agent ){
return false;
}
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_USERAGENT, $agent);
curl_setopt($c, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));
curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');//这个是解释gzip内容.................
curl_setopt($c, CURLOPT_URL, $URL);
curl_setopt($c, CURLOPT_TIMEOUT,2);
$contents = curl_exec($c);
$contents = mb_convert_encoding($contents, 'utf-8', 'GBK,UTF-8,ASCII');
$httpCode = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
return ['data'=>$contents,'http_code'=>$httpCode];
}