简体   繁体   English

我可以在PHP中禁用DNS查找吗? 我有IP地址和主机名

[英]Can I disable DNS lookup in PHP? I have the IP address and host name

We have mirrors of a website using the same domain name but different IP addresses - it's used for caching worldwide. 我们有一个使用相同域名但不同IP地址的网站镜像 - 它用于全球缓存。

To check the mirror is working, in theory we can use: 要检查镜像是否正常工作,理论上我们可以使用:

$request = new HttpRequest();
...
// IP address of mirror
$request->setUrl('http://xxx.xxx.xxx.xxx');
$request->setHeaders(array('host' => 'domainname.com'));
...
$request->send();

But this gives an error saying "'HttpRequestException' with message 'Couldn't resolve host name; ..." 但是这会给出一个错误,说''HttpRequestException',消息'无法解析主机名; ...“

We already know the IP address and host name, so is there a way to disable the DNS lookup in PHP? 我们已经知道IP地址和主机名,那么有没有办法在PHP中禁用DNS查找? Or in the http request headers? 或者在http请求标头中?

Note: 注意:

I need to login and have cookies, so I can't use ping. 我需要登录并拥有cookie,所以我不能使用ping。 Also the website needs to think its the correct domain, which is why I'm using 'Host' in the header. 此外,网站需要认为它是正确的域名,这就是为什么我在标题中使用'主机'。

UPDATE 1: 更新1:

Tried replacing HttpRequest with curl but this gives the same error message: 尝试用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'
}

UPDATE 2: 更新2:

Ah... It looks like its because of a redirect. 啊......看起来像是因为重定向。 I also have the following settings 我也有以下设置

curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

From the command line, this works 从命令行,这是有效的

curl --header "Host: domainname.com" http://xxx.xxx.xxx.xxx

But it doesn't when I try to log in 但是当我尝试登录时却没有

curl --header "Host: domainname.com" --location --data "username=username&password=password" http://xxx.xxx.xxx.xxx/login.php

I get 我明白了

curl: (6) Couldn't resolve host 'domainname.com'

Can I keep the ipaddress + host combination? 我可以保留ipaddress +主机组合吗? Or maybe I have to loop through the redirects? 或许我必须循环重定向?

Dont know about HttpRequest, but you can try to use CURL like this: PHP cURL doesn't see the /etc/hosts file 不知道HttpRequest,但你可以尝试使用这样的CURL: PHP cURL没有看到/ etc / hosts文件

Just set URL as IP address, and Host header as domain name and it should work as needed. 只需将URL设置为IP地址,将Host头设置为域名,它应该可以根据需要工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM