CURL does not work sometimes and gives an empty result

I used cURL to get data from another site. Sometimes it shows data, and sometimes an 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;

+5
source share
3 answers

Curl in PHP returns null to AWS EC2 instance

I had a similar problem, and I fixed it, making sure that the versions and settings in the php.ini file located in the PHP5 and Apache2 folders were the same. If it is not, then Apache2 tries to execute the versions installed inside php.ini settings. Also make sure PHP5-libcurl is installed. This is also important.

0
source

Perhaps intermittent failures are caused by vulnerable DNS servers, even if it is not, you are still using Google DNS servers. See: https://developers.google.com/speed/public-dns/docs/using

0
source

Perhaps you are calling too many connections from your curl_init to the same IP address, so the server blocks the connection and causes on / off errors.

0
source

All Articles