0

I need to Curl from a PHP website to a host that's unresolvable from the target machine (curl_error returns (6) couldn't resolve host). It's resolvable just fine on my local machine though, so after some web searching I figured it might be a DNS caching issue.
I only have FTP access to this webserver, so I can't restart Apache or edit the r.conf file.
I tried Curling using the IP address I got from pinging the domain, but unfortunately I need to make requests to a subdomain (e.g., api.domain.com), and requests to the IP address directly get handled differently.

I had hoped I could specify the subdomain+domain in the Host header (see below), but this doesn't work either.

$curl = curl_init();

$opt = array(
    CURLOPT_URL => 'http://11.22.33.44/handler.php?params=1',
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_HTTPHEADER => array('Host: subdomain.domain.com')
);
curl_setopt_array($curl, $opt);
$output = curl_exec($curl);
2
  • Can you elaborate on how this "doesn't work?" Commented Nov 17, 2014 at 10:51
  • It doesn't work, as it seems as if the Host header is ignored. I get the same result without it. Commented Nov 17, 2014 at 10:53

1 Answer 1

1

An alternative way to the Host: fiddling (if you use a new enough PHP version, apparently this is in 5.5 or later) is to pre-populate the libcurl DNS cache with a "fake" entry for the host name and then you can use the host name in the URL.

See the CURLOPT_RESOLVE option which seems badly documented in the PHP docs but can be found out about in the bug tracker: https://bugs.php.net/bug.php?id=63488&edit=1

The underlying libcurl option CURLOPT_RESOLVE is documented on the curl web site.

Sign up to request clarification or add additional context in comments.

4 Comments

Badly documented indeed, but this looks promising!
Unfortunately, using CURLOPT_RESOLVE I still get the same Curl error. I used the code snippet on that bug tracker page—once with the subdomain and once without—but neither had any noticeable effect.
Then investigate exactly how the request looks like and make sure it is correct, because both these methods should work
Thanks to that suggestion, I got it to work with the Host: fiddling! Still no idea why CURLOPT_RESOLVE does nothing, but if you hadn't said the Host header should work too, I might have given up! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.