【问题标题】:Can I disable DNS lookup in PHP? I have the IP address and host name我可以在 PHP 中禁用 DNS 查找吗?我有 IP 地址和主机名
【发布时间】:2013-12-19 10:40:22
【问题描述】:

我们有一个网站的镜像,使用相同的域名但不同的 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请求头中?

注意:

我需要登录并拥有 cookie,所以我无法使用 ping。此外,网站需要认为它是正确的域,这就是我在标题中使用“主机”的原因。

更新 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 组合吗?或者也许我必须遍历重定向?

【问题讨论】:

  • 为什么你使用主机头? ping 还不够吗?在最坏的情况下,在端口 80 上发出 TCP-IP 请求。
  • 谢谢鲍里斯,但我也必须登录 - 我先登录然后查看页面是否存在 - 而且该网站只能使用正确的域名而不是 IP 地址。

标签: php dns httprequest


【解决方案1】:

不知道 HttpRequest,但你可以尝试像这样使用 CURL: PHP cURL doesn't see the /etc/hosts file

只需将 URL 设置为 IP 地址,将 Host 标头设置为域名即可。

【讨论】:

猜你喜欢
  • 2010-09-11
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
相关资源
最近更新 更多