Php – cURL not working sometimes and gives empty result


I used cURL to get data from another website. Sometimes it shows data and sometimes empty result

Here is my Code

    function get_data($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_USERAGENT, $agent); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);

        $data = curl_exec($ch);
        return $data;
    $returned_content = get_data('');
    echo $returned_content;

Best Solution

Possibly you call too many connections from your curl_init to one Ip Address, So the server blocks the connection and causes the on/off errors.

Related Question