- curl 模拟浏览器发起请求,支持 GET 和 POST 方法
function httpRequest($url, $data = '') {
$header = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
if (empty($data) == false) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
2. 获取网页内容并匹配
function httpRequestMatch($url, $regular) {
$header = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
return [];
}
preg_match_all($regular, $data, $matches);
return $matches;
}
3. 其他选项
// 使用 HTTP 代理
// curl_setopt($curl, CURLOPT_PROXY, 'http://192.168.1.10:3128');
// 使用 Socks5 代理
// curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
// 发送 HEAD 请求,不返回 body
// curl_setopt($curl, CURLOPT_NOBODY, true);
// 准备上传
// curl_setopt($curl, CURLOPT_UPLOAD, true);
// 设置 Cookie
// curl_setopt($curl, CURLOPT_COOKIE, 'username=admin');
// 使用 Cookie 文件
// curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies.txt');
// 保存 Cookie 信息到本地
// curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookies.txt');
// 自定义请求类型
// curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET|POST');
// 压缩类型
// curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
// 使用公钥访问
// curl_setopt($curl, CURLOPT_PINNEDPUBLICKEY, 'public.pem');
// 设置用户代理
// curl_setopt($curl, CURLOPT_USERAGENT, 'Curl');
// 自定义域名解析
// curl_setopt($curl, CURLOPT_RESOLVE, ['example.com:80:127.0.0.1']);