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;
This means no HTTP headers or content, simply a closed TCP connection with no HTTP payload is transmitted. curl: (52) Empty reply from the server is a server related issue. However, this happens when libcurl did not receive any response from the server even after it has sent off its request.
Cause #1 – cURL is not enabled cURL is supported by your hosting company/plan but not enabled: If cURL is supported by you hosting company but it is not enabled by default, then often you simply just need to login to your hosting dashboard, navigate to the relevant section and enable it.
Create phpinfo. php file and save. phpinfo; ?> Then go to http://domainname/phpinfo.php to check whether CURL is enabled or not.
If you want to fetch the error message, make sure you fetch it before you close the current cURL session or the error message will be reset to an empty string. This is returned if CURLOPT_FAILONERROR is set TRUE and the HTTP server returns an error code that is >= 400.
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.
Not receiving any content back could be due to one or more out of many different reasons and you need to figure out which.
Use curl_error($ch)
in your code after the transfer has been performed to see if curl has told you about a problem.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With