我们有一个使用相同域名但不同 IP 地址的网站的镜像 - 它用于在全局范围内进行缓存。
要检查镜像是否正常工作,理论上我们可以使用:
$request = new HttpRequest();
...
// IP address of mirror
$request->setUrl('http://xxx.xxx.xxx.xxx');
$request->setHeaders(array('host' => 'domainname.com'));
...
$request->send();
但是这给出了一个错误,说“'HttpRequestException',消息'无法解析主机名; ...”
我们已经知道 IP 地址和主机名,那么有没有办法在 PHP 中禁用 DNS 查找?还是在 http 请求 header 中?
注意:
我需要登录并有 cookie,所以我不能使用 ping。此外,该网站需要考虑其正确的域,这就是我在 header 中使用“主机”的原因。
更新 1:
尝试用 curl 替换 HttpRequest,但这给出了相同的错误消息:
$curl = curl_init()
...
curl_setopt($curl, CURLOPT_URL, 'http://xxx.xxx.xxx.xxx');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host: domainname.com'));
...
if (!curl_exec($curl)) {
echo curl_error($curl);
// Output is 'Couldn't resolve host 'domainname.com'
}
更新 2:
啊...看起来是因为重定向。我还有以下设置
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
在命令行中,这是可行的
curl --header "Host: domainname.com" http://xxx.xxx.xxx.xxx
但是当我尝试登录时却没有
curl --header "Host: domainname.com" --location --data "username=username&password=password" http://xxx.xxx.xxx.xxx/login.php
我明白了
curl: (6) Couldn't resolve host 'domainname.com'
我可以保留 ipaddress + host 组合吗?或者我可能必须循环遍历重定向?
最佳答案
不知道 HttpRequest,但你可以尝试像这样使用 CURL: PHP cURL doesn't see the /etc/hosts file
只需将 URL 设置为 IP 地址,并将主机 header 设置为域名,它应该可以按需要工作。
关于php - 我可以在 PHP 中禁用 DNS 查找吗?我有 IP 地址和主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20350042/