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);
        $agent=$_SERVER["HTTP_USER_AGENT"];
        curl_setopt($ch,CURLOPT_USERAGENT, $agent); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);

        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    $returned_content = get_data('www.example.com');
    echo $returned_content;


可能你从curl_init调用了太多的连接到一个IP地址,所以服务器阻塞了连接并导致了开/关错误。


Curl in PHP returns null on AWS EC2 instance

I had similar issue and I fixed it by making sure versions and settings in php.ini file which is in PHP5 and Apache2 folders were same. If its not then Apache2 tries to execute versions set inside the php.ini settings. Also make sure PHP5-libcurl is installed. Its important too.


间歇性故障可能是由于片状DNS服务器造成的,即使不是这样,您仍然可以从使用Google的DNS服务器中受益。请参阅:https://developers.google.com/speed/public-dns/docs/运用

链接地址: http://www.djcxy.com/p/48546.html

上一篇: 如何让Tor服务使用Socket执行DNS解析?

下一篇: cURL有时无法工作,并给出空的结果