6
votes

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;

4
Try to check what happens when failing by using curl_error($ch); php.net/curl_errorgvgvgvijayan
For me is working well, maybe you have problem connecting.Alex
Probably has something to do with the page you're requesting, but you could try this funct instead cus it has some error handling: geneticcoder.blogspot.com/2015/02/curl-function-for-php.htmlI wrestled a bear once.
Try adding exit; after echo $returned_content;Ashwani Goyal
Btw www.example.com is not a URL, you are missing the protocol.Daniel W.

4 Answers

3
votes

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.

2
votes

Not receiving any content back could be due to one or more out of many different reasons and you need to figure out which.

  1. Use curl_error($ch) in your code after the transfer has been performed to see if curl has told you about a problem.

  2. Investigate the response headers. A HTTP transfer can be successful and not return any error but might only respond with HTTP headers and no body at all. The HTTP headers may then contain clues as to why. Perhaps it is a redirect you should follow, perhaps it requires HTTP authentication or perhaps the server indicates a temporary server glitch.

0
votes

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.

0
votes

Possibly the intermittent failures are due to flaky DNS servers, even if it isn't, you'll still benefit from using Google's DNS servers.. See: https://developers.google.com/speed/public-dns/docs/using