Joon. P
Joon. P

Reputation: 2298

PHP curl returns error: Couldn't resolve host 'www.example.com'. How can I solve this?

this function returns null and prints error :

Couldn't resolve host 'www.example.com'

function file_get_html_using_cURL($url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $output = curl_exec($ch);
    if(curl_errno($ch)){
        echo 'Curl error: ' . curl_error($ch);
        echo "\n--------------------\n";
        print_r(curl_getinfo($ch));
        echo "\n--------------------\n";
    }
    $output = str_get_html($output); // <-- Important line to convert string into object!
    curl_close($ch);
    return $output;
}

When I load this code in localhost, it works. But it throws the error when I upload it to the remote server.

I am guessing that the remote server has blocked CURL from executing.

It's a free server so I can't change any settings in php.ini

Is there any way around this?

P.S.

file_get_content($url)

is blocked as well. So I tried using curl instead. But not curl doesn't seem to work anymore.

Upvotes: 0

Views: 11019

Answers (1)

Arshid KV
Arshid KV

Reputation: 10037

Please check following code and verify your server issue.

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

var_dump(curl_exec($curl));
var_dump(curl_getinfo($curl));
var_dump(curl_error($curl)); 

Upvotes: 0

Related Questions