PHP使用curl模拟浏览器访问
来源:互联网 作者:幕尘枫 浏览:101次 发表时间:2022-6-30 16:22:02
/**
* get请求
* @param $url
*/
function curl_get($url,$gzip=false,$firefox=false) {
if($firefox) {
//火狐浏览器
$useragent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0';
} else {
//谷歌浏览器
$useragent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36';
}
$header = FormatHeader($url,$useragent);
$timeout= 120;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
//设置请求头信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//不取得返回头信息
curl_setopt($ch, CURLOPT_HEADER, 0);
if($gzip) {
//解释gzip加密压缩
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
}
// 关闭https验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_ENCODING, "" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_AUTOREFERER, true );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
//添加请求头
function FormatHeader($url,$useragent) {
// 解析url
$temp = parse_url($url);
$query = isset($temp['query']) ? $temp['query'] : '';
$path = isset($temp['path']) ? $temp['path'] : '/';
$header = array (
"POST {$path}?{$query} HTTP/1.1",
"Host: {$temp['host']}",
"Referer: http://{$temp['host']}/",
"Content-Type: text/xml; charset=utf-8",
'Accept: application/json, text/javascript, */*; q=0.01',
'Accept-Encoding:gzip, deflate, br',
'Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Connection:keep-alive',
'X-Requested-With: XMLHttpRequest',
'User-Agent: '.$useragent,
);
return $header;
}
————————————————
版权声明:本文为CSDN博主「幕尘枫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq15577969/article/details/124452866
* get请求
* @param $url
*/
function curl_get($url,$gzip=false,$firefox=false) {
if($firefox) {
//火狐浏览器
$useragent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0';
} else {
//谷歌浏览器
$useragent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36';
}
$header = FormatHeader($url,$useragent);
$timeout= 120;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
//设置请求头信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//不取得返回头信息
curl_setopt($ch, CURLOPT_HEADER, 0);
if($gzip) {
//解释gzip加密压缩
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
}
// 关闭https验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_ENCODING, "" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_AUTOREFERER, true );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
//添加请求头
function FormatHeader($url,$useragent) {
// 解析url
$temp = parse_url($url);
$query = isset($temp['query']) ? $temp['query'] : '';
$path = isset($temp['path']) ? $temp['path'] : '/';
$header = array (
"POST {$path}?{$query} HTTP/1.1",
"Host: {$temp['host']}",
"Referer: http://{$temp['host']}/",
"Content-Type: text/xml; charset=utf-8",
'Accept: application/json, text/javascript, */*; q=0.01',
'Accept-Encoding:gzip, deflate, br',
'Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Connection:keep-alive',
'X-Requested-With: XMLHttpRequest',
'User-Agent: '.$useragent,
);
return $header;
}
————————————————
版权声明:本文为CSDN博主「幕尘枫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq15577969/article/details/124452866
扫一扫 在手机阅读、分享本文
下一篇:一个因“快感”而崛起的暴利行业
版权所有:《董为坚个人网》
本文地址:https://www.dwjgrw.cn/Content/id/257.html 复制地址
本站内容除特别注明外,均为《董为坚个人网》原创,欢迎转载!转载请注明本文地址,谢谢!
相关内容
- 2022-7-22 10:41:08帝国CMS图集PHP调用代码及调用图集第一张图片
- 2022-7-2 9:42:57灵动标签月点击排行榜
- 2022-7-1 20:17:04单页面(自定义页面)导航高亮
- 2022-6-26 12:13:05帝国6.6/7.0/7.2 JS调用登陆状态模板会员
- 2022-6-20 20:19:41列表内容模板(list.var)中显示信息来源
发表评论 [评论会通过邮件和短信的形式通知我,我会及时回复,一定回来看哦!!!]
暂无评论