49

I'm trying to fetch the contents of a page using CURL. The page that is doing the fetching is https and the page it is trying to fetch is also https. I'm getting an error "Couldn't resolve host" with all of the settings I try.

$c=curl_init();
curl_setopt($c, CURLOPT_URL,$url);
//curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x");
curl_setopt ($c, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($c, CURLOPT_SSL_VERIFYPEER, TRUE);
//curl_setopt($c, CURLOPT_SSL_VERIFYHOST, TRUE);
curl_setopt($c, CURLOPT_HEADER, FALSE);

$html=curl_exec($c);

if($html === false) {
    echo curl_error($c);
}
else {
    echo 'Operation completed without any errors';
}   


curl_close($c);

Any ideas?

swt83
  • 2,001
  • 4
  • 25
  • 33

11 Answers11

64

I found that curl can decide to use IPv6, in which case it tries to resolve but doesn't get an IPv6 answer (or something to that effect) and times out.

You can try the curl command line switch -4 to test this out:

curl -4 http://x.com

In PHP, you can configure this line by setting this:

curl_setopt($c, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

Official manual page for this option: https://curl.se/libcurl/c/CURLOPT_IPRESOLVE.html

Jacob Bruinsma
  • 1,087
  • 1
  • 10
  • 23
  • 4
    Just an FYI. Had an issue with PHP Curl not working. Everything else on the server was working fine. Added these options and fixed it. The strange thing is that everything was working fine without any changes until recently. Obscure issue for sure but glad this worked! – Valien Mar 21 '17 at 19:53
  • 2
    This just started happening for me as well. Wasted 5 hours of my life... – Justin Levi Winter Mar 22 '17 at 01:02
  • 5
    Not sure what happened yesterday (21st of March 2017) but we experienced this problem on multiple servers to running PHP 7. Problem was solved after adding this line to the Curl request and restarting PHP-FPM. – Yo-han Mar 22 '17 at 08:39
  • 3
    Yesterday (21st March 17) we also found servers stopped working with DNS lookups for curl, really strange. Restart of php5.6-fpm worked then failed again a day later (today), will put the ipv4 dns option in and test when it fails again. – morleyc Mar 22 '17 at 09:56
  • Just had this problem (22 March 17), services randomly stopped working. After changing settings to include this services have started working again. Really strange as to why CuRL/the server would suddenly do this. – LewisJWright Mar 22 '17 at 10:58
  • I had the same problem 21st of march 2017 with php7 using apache's mod_php. The problem was solved after restarting apache. – acidtv Mar 22 '17 at 11:02
  • 1
    Update: so far this seems to be related to ubuntu auto-updating its libc-bin package. Bug on php.net: https://bugs.php.net/bug.php?id=74287 – acidtv Mar 22 '17 at 11:26
  • 9
    The Ubuntu bug is [fixed](https://bugs.launchpad.net/ubuntu/+source/eglibc/+bug/1674532). Check your apt logs to see if it's updated yet with `cat /var/log/apt/history.log`. If not you probably need to run a `apt-get update` and `apt-get upgrade`. A restart of PHP-FPM will do the trick after the fix. – Yo-han Mar 22 '17 at 12:18
  • @Yo-han et al, thank you for the info. Restarting services only temporarily fixed the issue but we'll have to see if the Ubuntu updates permanently solve issues. In meantime, I'll be forcing IPv4 – Rob W Mar 22 '17 at 13:44
  • Confirmed here on Ubuntu 14.04.5 Started 2017-03-21 after automatic update of libc6 2.19 – Olivier Langelaar Mar 22 '17 at 14:14
  • Ran into this problem today while using Laravel 5.2 and PHP7. Running curl through Laravel didn't work but running it through a standalone script DID. Adding this option in Laravel helped (must admit I have no idea why and sadly have no time to investigate this). – prismus Mar 22 '17 at 15:23
  • It is March 9, 2019 and the OP just started occurring for me also on production cPanel server. Adding `CURL_IPRESOLVE_V4` fixed it immediately. After one use, this option was not needed again. – David Mar 09 '19 at 17:14
  • @David, I tried this line of code as well and this didn't solve it for me. Did you change anything else by chance? –  Jun 19 '19 at 14:00
39

Maybe a DNS issue?

Try your URL against this code:

$_h = curl_init();
curl_setopt($_h, CURLOPT_HEADER, 1);
curl_setopt($_h, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($_h, CURLOPT_HTTPGET, 1);
curl_setopt($_h, CURLOPT_URL, 'YOUR_URL' );
curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );

var_dump(curl_exec($_h));
var_dump(curl_getinfo($_h));
var_dump(curl_error($_h)); 
KB22
  • 6,899
  • 9
  • 43
  • 52
34

I had the same problem. Coudn't resolve google.com. There was a bug somewhere in php fpm, which i am using. Restarting php-fpm solved it for me.

TecBeast
  • 930
  • 8
  • 16
6

Just a note which may be helpful- I was having this trouble with Apache on my laptop (which connects by wifi AFTER startup), and restarting the server (after connect) fixed the issue. I guess in my case this may be to do with apache starting offline and perhaps there noting that DNS lookups fail?

Rick
  • 93
  • 1
  • 5
2

There is a current bug in glibc on Ubuntu which can have this effect: https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1674733

To resolve it, update libc and all related (Packages that will be upgraded: libc-bin libc-dev-bin libc6 libc6-dev libfreetype6 libfreetype6-dev locales multiarch-support) and restart the server.

2

After tried all above, still can't resolved my issue yet. But got new solution for my problem.

At server where you are going to make a request, there should be a entry of your virtual host.

sudo vim /etc/hosts

and insert

192.xxx.x.xx www.domain.com

The reason if you are making request from server to itself then, to resolve your virtual host or to identify it, server would need above stuff, otherwise server won't understand your requesting(origin) host.

Jigar7521
  • 1,549
  • 14
  • 27
0

We need to add host security certificate to php.ini file. For local developement enviroment we can add cacert.pem in your local php.ini.

do phpinfo(); and file your php.ini path open and add uncomment ;curl.capath

curl.capath=path_of_your_cacert.pem

Prince
  • 31
  • 2
  • 6
  • Unfortunately your post is covering a different issue. I don't think this solution relates to the "*Cannot resolve host*" error. – Simon East Jun 20 '17 at 00:23
0

If you do it on Windows XAMPP/WAMP it probaly won't work as in my case.

I solved the problem setting up Laravel's Homestead/Vagrant solution to create my (Ubuntu) development environment - it has built-in: Nginx, PHP 5.6, PHP 7.3, PHP 7.2, PHP 7.1, MySQL, PostgreSQL, Redis, Memcached, Node... to name just a few.

See here for info how to set up the environment - it's really worth the effort!

Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine. No more worrying about messing up your operating system! Vagrant boxes are completely disposable. If something goes wrong, you can destroy and re-create the box in minutes!

Then you can easily switch PHP versions or set up more virtual hosts, new databases just in seconds.

Picard
  • 3,745
  • 3
  • 41
  • 50
-3

You may have to enable the HTTPS part:

curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST,  2);

And if you need to verify (authenticate yourself) you may need this too:

curl_setopt($c, CURLOPT_USERPWD, 'username:password');
Mr. Smith
  • 5,489
  • 11
  • 44
  • 60
-3

Your getting the error because you're probably doing it on your Local server environment. You need to skip the certificates check when the cURL call is made. For that just add the following options

curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST,  0);
-3

add yourlocalhost
ex. 127.0.0.1 cards.localhost in the /etc/hosts directory. Now restart apache server

sunil
  • 1
  • 1