Php cURL not working

apache-2.2curlPHP

The php cURL extension on the Apache server I've inherited from my predecessor doesn't work.

I'm using the following code:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.linkedin.com/nhome/');
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_STDERR, $fp);

    $response = curl_exec($ch);
    $errmsg = curl_error($ch);
    $cInfo = curl_getinfo($ch);
    curl_close($ch);

Which returns the error:

* name lookup timed out
* Couldn't resolve host 'www.linkedin.com'
* Closing connection #0

However if I use an IP address instead of an URL it works:

* About to connect() to 54.171.54.110 port 80
*   Trying 54.171.54.110... * connected
* Connected to 54.171.54.110 (54.171.54.110) port 80

I'm assuming this is a DNS issue? How should I begin to troubleshoot the problem?

Best Answer

Thanks to ThoriumBR's comment for a push in the right direction.

The nameserver listed in /etc/resolv.conf wasn't working, so I replaced it with google's public nameservers:

nameserver 8.8.8.8
nameserver 8.8.4.4
Related Topic