Modifying an existing scrip to grab this text:Solar Discussion. I am getting the text to here: solar-discussion.txt but not here: solar-discussion.php. The code is below. Can't figure out why its not reading the cached file.
Thanks for any help on this, Doug
Code:
<?php
// PHP script by Ken True, webmaster@saratoga-weather.org
// you may copy/modify/use this script as you see fit,
// no warranty is expressed or implied.
$cacheName = "solar-discussion.txt"; // used to store the file so we don't have to
// fetch it each time
$refetchSeconds = 1800; // refetch every nnnn seconds
// end of settings
// Constants
// don't change $fileName or script may break ;-)
$fileName = "http://www.swpc.noaa.gov/ftpdir/latest/RSGA.txt";
// end of constants
// ------ start of code -------
if (isset($_REQUEST['sce']) && strtolower($_REQUEST['sce']) == 'view' ) {
//--self downloader --
$filenameReal = __FILE__;
$download_size = filesize($filenameReal);
header('Pragma: public');
header('Cache-Control: private');
header('Cache-Control: no-cache, must-revalidate');
header("Content-type: text/plain");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size");
header('Connection: close');
readfile($filenameReal);
exit;
}
// Check parameters and force defaults/ranges
if ( ! isset($_REQUEST['inc']) ) {
$_REQUEST['inc']="";
}
if (isset($doIncludeFD) and $doIncludeFD ) {
$includeMode = "Y";
} else {
$includeMode = $_REQUEST['inc']; // any nonblank is ok
}
if ($includeMode) {$includeMode = "Y";}
if (isset($_REQUEST['cache'])) {$refetchSeconds = 1; }
// omit HTML <HEAD>...</HEAD><BODY> if only tables wanted
// --------------- customize HTML if you like -----------------------
if (! $includeMode) {
?>
<?php
}
// ------------- code starts here -------------------
echo "<!-- $Version -->\n";
// refresh cached copy of page if needed
// fetch/cache code by Tom at carterlake.org
if (file_exists($cacheName) and filemtime($cacheName) + $refetchSeconds > time()) {
print "<!-- using Cached version of $cacheName -->\n";
$html = implode('', file($cacheName));
} else {
print "<!-- loading $cacheName from $fileName -->\n";
$html = fetchUrlWithoutHangingFD($fileName);
$fp = fopen($cacheName, "w");
if ($fp) {
$write = fputs($fp, $html);
fclose($fp);
print "<!-- cache written to $cacheName. -->\n";
} else {
print "<!-- unable to save cache to $cacheName. -->\n";
}
}
// extract the solar discussion
preg_match('|<pre[^>]*>(.*)</pre>|Usi',$html,$matches);
$discussion = $matches[1]; // now have the forecast as a string with \n delimiters
$discussion = trim($matches[1]); // prevent extra white space at beginning and end
// uncomment next line if you like only lower case text, or it could be made an option
$discussion = strtolower(trim($matches[1]));
if ($discussion == '') {
$discussion = 'Solar discussion unavailable, please try later.';
}
print "<pre>\n";
print htmlspecialchars(strip_tags($discussion));
print "</pre>\n"; $niceFileName = preg_replace('!&!is','&',$fileName);
print "<p><small><a href=\"$niceFileName\">Report of Solar-Geophysical Activity</a></small></p>\n";
// print footer of page if needed
// --------------- customize HTML if you like -----------------------
if (! $includeMode ) {
?>
<?php
}
// ----------------------------functions -----------------------------------
function fetchUrlWithoutHangingFD($url) // thanks to Tom at Carterlake.org for this script fragment
{
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
$numberOfSeconds=6;
// Suppress error reporting so Web site visitors are unaware if the feed fails
error_reporting(0);
// Extract resource path and domain from URL ready for fsockopen
$url = str_replace("http://","",$url);
$urlComponents = explode("/",$url);
$domain = $urlComponents[0];
$resourcePath = str_replace($domain,"",$url);
// Establish a connection
$socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds);
if (!$socketConnection)
{
// You may wish to remove the following debugging line on a live Web site
print "<!-- Network error: $errstr ($errno) -->\n";
} // end if
else {
$xml = '';
fputs($socketConnection, "GET $resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n");
// Loop until end of file
while (!feof($socketConnection))
{
$xml .= fgets($socketConnection, 4096);
} // end while
fclose ($socketConnection);
} // end else
return($xml);
} // end function
// --------------end of functions ---------------------------------------
?>