Forum Moderators: coopster
$url = "http://www.whatever.com/rss.xml";
$filename = "/home/example/data/file.dat";
if (!is_file($filename)) $difference = 901;
else $difference = time() - filemtime($filename);
if ($difference > 900) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
$contents = curl_exec($ch);
curl_close($ch);
//// This is the old script
// if (!$contents = file_get_contents($url)) {
// $error = error_get_last();
// echo "HTTP request failed: " . $error['message'];
// }
if ($contents) {
// print data from XML feed
}
} $ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
$contents = curl_exec($ch);
curl_close($ch); curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_STDERR, fopen('/path/to/log/file.txt', 'w')); $url = 'http://www.whatever.com/rss.xml';
$filename = '/home/example/data/file.dat';
$error_log = 'error.log';
if (!is_file($filename)) $difference = 901;
else $difference = time() - filemtime($filename);
if ($difference > 900) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
// Error log
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, fopen($error_log, 'a'));
$contents = curl_exec($ch);
curl_close($ch);
// I think this is needed, unless it's implied with STDERR?
fclose($error_log);
// Backup; in case cURL fails, try file_get_contents()
// I'll remove this once I update to PHP 5.6.29
if (!$contents) {
$contents = file_get_contents($url);
$msg = "\n** Moved on to file_get_contents()...\n ";
if (error_get_last()) {
$msg .= error_get_last();
$fh = fopen($error_log, 'a');
fwrite($fh, $msg);
fclose($error_log);
}
}
if ($contents) {
// print data from XML feed
}
}
Just for clarification, though, am I right that there's no need for an fwrite() statement anywhere? And, do I need to fclose('file.txt') after curl_close($ch), or is it automatically done with the curl_setopt()?
I use the file.dat to run this when someone visits the page and it hasn't been updated in > 15 minutes
Curl will write to the file if any errors occur, and you can close the file explicitly, as you do, but since it's a short-running script, all opened files will be closed upon exiting anyway.
Note that, if it has been >15 minutes, you're increasing your users' load time that way. If the download gets stuck for some reason, they may even have to wait 60 seconds for the job to time out.