Php curl returning blank page for some sites where curl command working fine.
for example : curl www.wikipedia.org generating output but php curl giving blank pages with <html>
tags
$ch = curl_init(); // initialize curl with given url
//TBD: all setopt commands return true/false. Should be handled
//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent
$res = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
curl_setopt($ch, CURLOPT_FAILONERROR, 0); // stop when it encounters an error
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // iNetClean is web-crawling, no need to verify certificates
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_ENCODING, ""); // accept all encodings - identity, deflate, gzip
// now set the URL
curl_setopt($ch, CURLOPT_URL, $url);
$fp_out = fopen($html_file, 'w');
if (!$fp_out) {
if ($DEBUG) {
error_log("couldn't create " . $html_file);
}
} else {
if ($DEBUG) {
error_log("file created " . $html_file);
}
}
$fp_err = fopen($html_err_file, 'w');
if (!$fp_err) {
if ($DEBUG) {
error_log("couldn't create " . $html_err_file);
}
} else {
if ($DEBUG) {
error_log("error file created " . $html_err_file);
}
}
curl_setopt($ch, CURLOPT_FILE, $fp_out); //rawurlencode($url) . "txt"); // for debugging only
curl_setopt($ch, CURLOPT_STDERR, $fp_err);
$result = curl_exec($ch);
//0 size file is created if no data is downloaded or URL does not exist such as pron00.com. Hence added handler to such errors.
if (@filesize($html_file) > 0) {
//file exists and contain some data
} else {
return false;
}
if ($result == false) {
trigger_error(curl_error($ch));
if ($DEBUG) {
error_log("Curl_exec fail");
}
return false;
}
fclose($fp_out);
fclose($fp_err);
curl_close($ch);
return $result;