//获取 cookie
public function iniCookie()
{
//访问AF主页, 获取session_id 以及其他必传cookie
$header = $this->curl_post('需要爬的网址', [])['header'];
//更新Cookie
$splited_header = explode("\r\n", $header);
$this->cookie = 'Cookie:';
foreach ($splited_header as $value) {
if (strstr($value, 'Set-Cookie:')) {
$value = str_replace('Set-Cookie:', '', $value);
$value = explode(';',$value)[0];
$this->cookie .= $value . ';';
}
}
//后续如果多页面 ,对应参数去模拟真实请求并跳转(按实践情况), 否则爬不到会报错
$this->curl_post(url,[参数]);
}
public function curl_post($url, $data)
{
$ch = curl_init();
//设置cookie
curl_setopt($ch, CURLOPT_HTTPHEADER, [$this->cookie]);
//设置返回结果为流
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//设置返回header
curl_setopt($ch, CURLOPT_HEADER, true);
/* 设置超时时间*/
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
/* 设置通信方式 */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$return = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($return, 0, $header_size);
$content = substr($return, $header_size);
curl_close($ch);
return [
'header' => $header,
'content' => $content
];
}
//请求
function curl($data , $path = ''){
ini_set('max_execution_time', '0');
$method = "POST";
$headers = array();
//array_push($headers, "Content-Type".":"."text/xml; charset=UTF-8");
$bodys = $data;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $path);
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
//curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
//https 访问屏蔽证书
if (1 == strpos("$".$path, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
$res = curl_exec($curl);
// 返回最后一次的错误号
$err_code = curl_errno($curl);
curl_close($curl);
if($err_code) {
var_dump($err_code);
var_dump($path); die;
}
return $res;
}