From:http://digdeeply.org/archives/10132139.html
When we develop tests, sometimes the Web server binds a domain name, but because DNS is unresolved, we need to set the host file to access it.
However, if we are required to access through curl, we cannot access the URL's host hosts. Therefore, it needs to be accessed by specifying the host, with the following specific methods:
If it's a Curl command under Linux:
Example
1 |
curl --silent -H "Host: www.digdeeply.info" "192.168.0.1/index.php" |
If you use PHP's curl, use curl_setopt to set up Curlopt_httpheader.
Please refer to the following function for use:
Example
123456789101112131415161718192021 |
//httpHeader 设置的 http head 参数 数组形式 如 array(‘Host: digdeeply.info‘)
function curl_by_host(
$url
,
$postString
=
‘‘
,
$httpHeader
=
‘‘
)
{
$ch = curl_init();
curl_setopt(
$ch
,CURLOPT_URL,
$url
);
curl_setopt(
$ch
,CURLOPT_POSTFIELDS,
$postString
);
curl_setopt(
$ch
,CURLOPT_RETURNTRANSFER,true);
curl_setopt(
$ch
,CURLOPT_USERAGENT,
$_SERVER
[
‘HTTP_USER_AGENT‘
]);
if
(!
empty
(
$httpHeader
) &&
is_array
(
$httpHeader
))
{
curl_setopt(
$ch
, CURLOPT_HTTPHEADER,
$httpHeader
);
}
$data = curl_exec(
$ch
);
$info = curl_getinfo(
$ch
);
curl_close(
$ch
);
if
(curl_errno(
$ch
)){
return $info
;
}
return $data
;
}
|
[Go]php Curl Set host curl_setopt Curlopt_httpheader specify host