codeigniter - Could not resolve host - Can't get cURL to work with php on nginx
I have Digitalocean LEMP application dropplet configured to host my website (PHP - CodeIgniter application), I'm using Nexmo SMS API in my application and everything worked well until today. Suddenly, now when I try to send SMS I have this error "Could not resolve host: rest.nexmo.com". Looks like cURL is not working, but when I checked if it is installed - it is, and my phpinfo shows it as well. I restarted nginx, php5-fpm, tried some different curl settings in my code but always got this error.
When I tried to run simple script like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/');
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
curl_exec($ch);
if ($error = curl_error($ch)) {
echo "Error: $error<br />\n";
}
function read_header($ch, $string)
{
$length = strlen($string);
echo "Received $length bytes<br />\n";
return $length;
}
result is still "Error: Could not resolve host: www.google.com" so i think the problem is in cURL and not in my application code (i'm using NEXMO CodeIgniter library).
I tried everything that comes in my mind and now I'm running out of ideas, so every help is appreciated. Are there any special settings/things to do to connect/make cURL works with nginx that I'm missing? Maybe something in nginx.conf files, or do I need to open some ports, etc?
Answer
Solution:
Note that the error is "Could not resolve host". This points not to curl, but to the system resolver library. Curl does not, by itself, do DNS lookups, but instead uses the system standard methods, usually using libresolv. If you use a system call trace utility like strace you will see that the resolver is then controlled by /etc/nsswitch.conf, /etc/host.conf and /etc/resolv.conf. Your first point of call should be there. You can test that it is system and not curl by using a standard PHP file call, like:
This should return the body if it can resolve the host.