?php
/**
* 开始访问请求
* @param $url
* @return bool|string
*/
function fetch_url($url) {
$header = FormatHeader($url);
$useragent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0';
$timeout= 120;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
//设置请求头信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//不取得返回头信息
curl_setopt($ch, CURLOPT_HEADER, 0);
// 关闭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);
if(curl_errno($ch))
{
echo 'Error:' . curl_error($ch);
}
else
{
return $content;
}
curl_close($ch);
}
//添加请求头
function FormatHeader($url)
{
// 解析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',
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0',
'X-Requested-With: XMLHttpRequest',
);
return $header;
}
?>
到此这篇关于php的curl携带header请求头信息实现http访问的方法的文章就介绍到这了,更多相关php的curl请求头信息实现http访问内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!