# php dns not working (gethostbyname)

## nautical9

Got a PHP DNS-related problem that I can't figure out:

For some reason, my Apache 2.0.52 / PHP 5.0.3 install doesn't want to let me do DNS lookups in the php code, at least not in the traditional way, but it's also not giving me any hints whatsoever in the log files.  It just fails (very quickly).

But here's the kicker: some of the newer DNS functions are working (and of course, DNS is working everywhere else on the machine).

Here's a quick breakdown:curl_exec - always returns "Couldn't resolve host"

gethostbyname ('bugs.php.net') - always returns the hostname I pass to it (meaning it's failed according to the docs).But these work:checkdnsrr ('bugs.php.net') - returns 1 (true - meaning it found a record).

dns_get_record('bugs.php.net',DNS_ANY) - returns an array with all the proper DNS info.

I really need to use the "curl" library for grabbing some HTTPS pages remotely, or else I'd just use the newer DNS functions.  But every hostname I try to fetch with the curl libraries fails (but 'curl' on the command line works fine).  I could probably work around it with a nasty "exec" call to "curl", but I'd really rather avoid that.

I've also spent over an hour trying to track down others who have had this problem elsewhere on the Net, to no avail.

Any helpful pointers would be greatly appreciated.

-D.

----------

## ydleiF

Looking at the possible USE flags for 5.0.3, I'd just make sure you have curl and possibly sockets enabled. Other than that, I'm not very sure. Check out the other USE flags as well, it might be one of those.

----------

## nautical9

curl_exec() and gethostbyname() still can't seem to resolve DNS addresses for me, and I've given up trying to figure out how to properly fix it (thx for the advice re: USE flags, but I already have both set).

In case other people have this problem (and dns_get_record() is working), a simple workaround is:

```

// First get IP address of the hostname

$dns = dns_get_record ('www.example.com', DNS_A);

// Make sure it worked

if (! isset ($dns[0]['ip'])) {

  // error - no results for DNS lookup

}

// Build the URL using the IP address

$url = "http://" . $dns[0]['ip] . "/path/to/whatever.html";

// Use curl to get the remote page

$ch = curl_init ($url);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

$results = curl_exec ($ch);

// Make sure we got a page

if (curl_errno ($ch) || ! $results) {

  // error - display with curl_error ($ch)

}

// Page is in $results

echo "$results";

```

Not as clean as just requesting the full path in one line, but at least it works without hard-coding an IP address.  Also for HTTPS connections, make sure to curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0), since the common-name of the cert won't match the IP address.

----------

