Forum Moderators: phranque
<script>
var newsPath = home + '/news.php?feed=national',
newsLocalPath = home + '/news.php';
$(function() {
if ($('#news_national').length)
natNews = setInterval("$('#news_national').ajax(newsPath)", 900000); // 15 min; 15 * 60 * 1000
if ($('#news_local').length)
locNews = setInterval("$('#news_local').ajax(newsLocalPath)", 3420000); // 57 min
});
</script>
<div id="news_national"></div>
<script>
$('#news_national').ajax(newsNationalPath);
</script>
<div id="news_local"></div>
<script>
setTimeout(function() {
$('#news_local').ajax(newsLocalPath)
}, 2500);
</script> function getFile($url, $getInfo) {
$t = false;
if (strpos($url, 'http') !== false) {
$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_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$t = curl_exec($ch);
// in this example I don't load $getInfo so this section shouldn't run
// I'm including it here, though, in case you see a problem
if ($getInfo && $t) {
$arr = curl_getinfo($ch);
$http = $arr['http_code'];
if ($http == 200)
$t = $arr[$getInfo];
else
$t = false;
}
curl_close($ch);
}
return $t;
}
// I'm leaving out code to select a list of news feeds from MySQL, but
// the logic is that the scripts reads them in order, then shows the
// first one that responds
while (list($news_id, $news_feed, $courtesy) = mysqli_fetch_row($sth_feed)) {
// I use a text file to store the last updated timestamp
$news_filename = $datapath . '/news/' . $news_id . '.dat';
if (is_file($news_filename))
$last_modified = time() - filemtime($news_filename);
if (!$last_modified ||
$last_modified > 900) // file is more than 15 minutes old)
$contents = getFile($news_feed);
else
break;
if ($contents) break;
else
mail('hostmaster@example.com',
"$courtesy isn't responding",
'body of the email, not relevant');
}
// blah blah blah
}