CLI cURL works flawlessly, Apache with mod_php cannot resolve host, how to fix?

1

0

Setup:

  • cURL 7.32.0
  • Apache 2.2.25
  • PHP 5.5.4

Whenever I attempt to:

$ch = curl_init('www.google.com');
curl_exec($ch);

var_dump(curl_error($ch));

within PHP, I get:

Could not resolve host: www.google.com

If I change to curl_init('80.233.168.207') (resolved google.com), the script executes.

Though, running curl www.google.com in CLI, I get a 302 Moved HTML page, due to redirect to localized site (*.lv), but still, that means that cURL executed successfully.

I have seen multiple similar questions (not on SE though), most of them saying that Apache is started either before or concurrently with Network Manager, hence not receiving DNS information. Most of the questions have answers explaining, that the issue can be temporarily resolved/debugged by explicitly stopping, then starting the Apache daemon (instead of restart/graceful-restart).

fsockopen, file_get_contents works flawlessly, though.

My Apache virtual host:

<VirtualHost *.777>
    ServerAdmin stone@altas.lv
    ServerName localhost

    DocumentRoot "/srv/stone/public"

    ErrorLog "/srv/stone/app/storage/logs/apache-errors.log"
    CustomLog "/srv/stone/app/storage/logs/apache-custom.log" common

    <Directory /srv/stone/public>
        php_admin_value open_basedir /

        # Pretty URL rewrites, better not to rely on .htaccess
        <IfModule mod_rewrite.c>
            Options -MultiViews

            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]
        </IfModule>

        # Prevent further overrides
        AllowOverride None

        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

And global config just has a specific entry for Listen 777.

This and this, both lead to DNS problem, so, I think I may have problems with it too, plus, it clearly looks like it.

I've given a shot by changing virtual host entries to 0.0.0.0:777 and other configurations to no avail.

How do I fix the issue?

joltmode

Posted 2013-09-25T07:30:31.583

Reputation: 609

If you try to put 80.233.168.207 www.google.com in your /etc/hosts you should be able to confirm that this is a DNS issue. It's a bit strange that your CLI command manage to perform the lookup though. FYI, the code works on my Apache installation. – Qben – 2013-09-25T11:20:33.957

Adding it to hosts did make the script work. – joltmode – 2013-09-25T11:33:44.370

Answers

1

It appears that the problem was with my /etc/resolv.conf.

I had set up a custom network connection manager, that executed an external command to set up nameservers from a specific configuration file. The setup would simply erase the contents of /etc/resolv.conf, append with new info and then start the network interface, set it's parameters (IP, Mask, Cast, Gate) and voila.

Apparently that screwed up something. I guess I will have to look for different Before/After targets and write a custom resolv.conf updater.

I did leave resolv.conf static now, and that seems to fix the issue.

In the end, clearly this was a DNS problem. Thankfully, it's fixed.

joltmode

Posted 2013-09-25T07:30:31.583

Reputation: 609

0

Have you tried the CURL_IPRESOLVE_V4 option?

$ch = curl_init('www.google.com');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_exec($ch);
var_dump(curl_error($ch));

And what is the output of the headers if you use the CURLOPT_HEADER option?

Rik

Posted 2013-09-25T07:30:31.583

Reputation: 11 800

FYI: curl_setopt has to come before curl_exec, but, It doesn't even get that far to gather headers, it simply cannot connect. Dies all the time with Could not resolve host. I did already try the IPRESOLVE_V4 option before, doesn't work. Did it now again, nothing... – joltmode – 2013-09-25T09:05:58.457

Oops, yes. That was a type. Had a working code here and it went wrong with copy and paste. – Rik – 2013-09-25T09:07:52.780

Why did you try 80.233.168.207. This is not the same as your www.google.com. Here the curl_info gives 74.125.132.106 as primary_ip on www.google.com. could you try curl_init('74.125.132.106')? What result do you get? Here 80.233.168.207 goes right to google.com without giving a page is moved. Did you try curl 80.233.168.207 and curl 74.125.132.106 in CLI? (this may all be a localisation issue but you can try) – Rik – 2013-09-25T10:11:16.503

Haha, both with curl -v 80.233.168.207 and curl -v 74.125.132.106 in CLI i get a 200 OK. With curl www.google.com i get a 302 Found. Same with Curl in PHP. ping www.google.com gives me 74.125.132.147. – Rik – 2013-09-25T10:28:09.320

That doesn't change the fact that I have a problem with DNS'es related to Apache, mod_php and curl on the machine. – joltmode – 2013-09-25T10:28:28.963

What does print_r(gethostbynamel('www.google.com')); show? Maybe Curl takes one of those (which can't resolve) while the CLI takes another. Do other sites work with curl? Or is it just the Google-ones? – Rik – 2013-09-25T10:31:01.580

gethostbynamel gives a bunch of 80.233.168.* entries. – joltmode – 2013-09-25T10:33:25.327

Maybe curl_init(gethostbyname('www.google.com')); works? Not that pretty though, and just hiding the root cause of the problem. :-) – Qben – 2013-09-25T11:43:18.807

Do other sites like www.microsoft.com also give this error? – Rik – 2013-09-25T13:34:39.033

And is giving host www.google.com on the CLI you the same numbers as print_r(gethostbynamel('www.google.com'));? – Rik – 2013-09-25T13:52:37.860