Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
php dns not working (gethostbyname)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
nautical9
n00b
n00b


Joined: 24 Feb 2003
Posts: 22
Location: Calgary, Alberta, Canada

PostPosted: Fri Mar 18, 2005 11:05 am    Post subject: php dns not working (gethostbyname) Reply with quote

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.
Back to top
View user's profile Send private message
ydleiF
Apprentice
Apprentice


Joined: 15 Nov 2002
Posts: 170
Location: Southeast Michigan, USA

PostPosted: Mon Mar 21, 2005 4:55 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
nautical9
n00b
n00b


Joined: 24 Feb 2003
Posts: 22
Location: Calgary, Alberta, Canada

PostPosted: Fri Mar 25, 2005 11:00 am    Post subject: Still stumped, but here's a quick workaround Reply with quote

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:

Code:

// 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.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum