3

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?

Russell England
  • 9,436
  • 1
  • 27
  • 41
  • Why u use host header? Wouldn't be ping enough? In worst case make TCP-IP request on port 80. – Boris Ivanov Dec 03 '13 at 11:42
  • Thanks Boris but I have to login too - I login first then see if the page exists - also the website will only work with the proper domainname rather than the IP address. – Russell England Dec 03 '13 at 11:55

1 Answers1

1

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.

Community
  • 1
  • 1
Serge Velikan
  • 1,131
  • 15
  • 31
  • Thanks Serge, I think curl might be the answer - just had a look at curl vs httprequest http://stackoverflow.com/questions/869927/php-difference-between-curl-and-httprequest - seems that httprequest uses curl anyway... – Russell England Dec 03 '13 at 12:14