Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
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 Sep 25 '13 at 11:20
    
Adding it to hosts did make the script work. –  joltmode Sep 25 '13 at 11:33
add comment

2 Answers

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?

share|improve this answer
    
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 Sep 25 '13 at 9:05
    
Oops, yes. That was a type. Had a working code here and it went wrong with copy and paste. –  Rik Sep 25 '13 at 9:07
    
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 Sep 25 '13 at 10:11
    
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 Sep 25 '13 at 10:28
    
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 Sep 25 '13 at 10:28
show 5 more comments
up vote 0 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.