php: fopen() of an URL breaks for domain names, not for numerical addresses

Posted by b0fh on Stack Overflow See other posts from Stack Overflow or by b0fh
Published on 2012-06-28T23:32:06Z Indexed on 2012/07/01 9:16 UTC
Read the original article Hit count: 457

Filed under:
|
|

After hours of trying to debug a third-party application having trouble with fopen(), i finally discovered that

php -r 'echo(file_get_contents("http://www.google.com/robots.txt"));'

fails, but

php -r 'echo(file_get_contents("http://173.194.32.81/robots.txt"));'

Succeeds. Note that as the webserver user, I can ping www.google.com and it resolves just fine.

I straced both executions of PHP, and they diverge like this:

For the numerical v4 URL:

socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
fcntl(3, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK)    = 0
connect(3, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("173.194
poll([{fd=3, events=POLLOUT}], 1, 0)    = 0 (Timeout)
...[bunch of poll/select/recvfrom]...
close(3)                                = 0

For the domain name:

socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 3
close(3)                                 = 0

PHP didn't even try to do anything with that socket, it seems. Or even resolve the domain, for that matter. WTF ?

Recompiling PHP with or without ipv6 support did not seem to matter. Disabling ipv6 on this system is not desirable.

Gentoo Linux, PHP 5.3.14, currently giving a try to PHP 5.4 and see if it helps. Anyone has an idea ?

EDIT:

php -r 'echo gethostbyname("www.google.com");'

Works and yield an ipv4, while

php -r 'echo(file_get_contents("http://[2a00:1450:4007:803::1011]/"));'

Seems to return a blank result.

EDIT 2:

I didn't even notice the first time, that the v6 socket opened when the name is used is a SOCK_DGRAM.

© Stack Overflow or respective owner

Related posts about php

Related posts about linux