Russell England
Russell England

Reputation: 10241

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.

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; ..."

We already know the IP address and host name, so is there a way to disable the DNS lookup in PHP? Or in the http request headers?

Note:

I need to login and have cookies, so I can't use ping. Also the website needs to think its the correct domain, which is why I'm using 'Host' in the header.

UPDATE 1:

Tried replacing HttpRequest with curl but this gives the same error message:

$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:

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? Or maybe I have to loop through the redirects?

Upvotes: 3

Views: 4190

Answers (1)

Serge Velikan
Serge Velikan

Reputation: 1171

Dont know about HttpRequest, but you can try to use CURL like this: PHP cURL doesn't see the /etc/hosts file

Just set URL as IP address, and Host header as domain name and it should work as needed.

Upvotes: 1

Related Questions