Major Problem with advforecast2.php script: NWS Site Redesign???

The icons are gone! The printable forcast is gone! NWS changed the site! This used to be for Glyndon, MD and now its gone!

http://forecast.weather.gov/MapClick.php?site=lwx&FcstType=text&MapType=3&site=lwx&CiTemplate=1&map.x=285&map.y=45&TextType=2

What is happening? Am I going crazy? It worked yesterday. The site was down for 2 hours. Now its all a different design.

Adam

Wow … you are right. Checked mine and also gone. Whole new forecast page at NWS … w/o Icons!!! Now what … we start all over?

I suspect when Ken sees this he’ll come up with something.

  • Jim

Gotta Love the NWS…

Lets change everything just for fun… :frowning:

-Bob

Man. I think we are SCREWED. Ken is the king of weather sites and scripts but I don’t see how he’ll pull a rabbit out of his arse for this one. The entire foundation for the script is gone!

Same here…I’m sure Ken will come up with something. Ken is a master at this forecast scripting.

Maybe no need for panic folks, that page currently has this message

“The NWS forecast web pages are currently defaulting to a zone based product due to technical difficulties. The point specific forecasts, with icons and the houly weather graph will return as soon as possible. Thanks for your patience.
NWS Baltimore, MD/Washington, D.C.”

Thank you Niko. I tried to find something like that statement. The page just does not look right in my browser. Does not fit right.

Well, that’s a new one for the transition… before they did a 302 (temporary redirect) to the zone forecast out of www.crh.noaa.gov … now they’re just being “helpful” and pulling the text only.

Let me see what I can do about detecting that, then force a failover to the REAL zone forecast (hopefully with icons).

Ken

BTW… THANKS for all the kind words! Let’s see if there’s a rabbit to find in this hat :lol:

Ok, here’s the quick fix to detect this and fail over to your REAL zone forecast.

Change:

	  if (preg_match('/Temporary|Location:/Uis',$html)) {

to

	  if (preg_match('/Temporary|Location:|defaulting to/Uis',$html)) {

in the TWO places it occurs in the source.

I’ve updated advforecast2.php (for the faint of heart doing PHP updates :wink: )

http://saratoga-weather.org/advforecast2.php?sce=view

Tested the failover with 1080iaddicts settings (http://saratoga-weather.org/advforecast2-testing.php ) and works fine.

Best regards,
Ken

Works like a charm Ken, thanks for your great coding. The Saratoga link does NOT work however!

Question though, is this an automatic check and re-direct and will go back to icon page when NWS fixes or we will have to change code again?

I’m a little confused (as always) with where to change it in my PHP Ajax code :frowning:

<?php
header("Expires: " . gmdate("D, d M Y H:i:s", time() - 150) . " GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s", time() - 150) . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

require_once 'getraw.php';
$wxdata = get_raw("wx30.html");
$thisPage="Forecast"; 

if ( empty($_REQUEST['force']) ) 
        $_REQUEST['force']="0";

$Force = $_REQUEST['force'];

// Get the forecast.txt file

$cacheName = "forecast.txt";
$fileName = "http://forecast.weather.gov/MapClick.php?site=BGM&llon=-75.847083&rlon=-74.969583&tlat=43.520417&blat=42.645417&smap=1&mp=1&map.x=126&map.y=108&TextType=2";

if ($Force==1) {
      $fileData = forecastfetchUrlWithoutHanging($fileName,$cacheName);
      $fp = fopen($cacheName, "w");
      $write = fputs($fp, $fileData);
      fclose($fp);  }

if (filemtime($cacheName) + 1800 > time()) { 
      $html = implode('', file($cacheName)); 
    } else { 
      $html = fetchUrlWithoutHanging($fileName,$cacheName); 
      $fp = fopen($cacheName, "w"); 
      $write = fputs($fp, $html); 
      fclose($fp);  } 

      // Just get the top of the NWS page for editing 

      preg_match('|<tr valign ="top" align="center">(.*)<table width="670"|s', $html, $betweenspan); 
      $forecastop  = $betweenspan[1]; 

      // Chop up each icon html and place in array 
      preg_match_all("/<td.*>(.*)<\/td>/Uis", $forecastop, $headers); 
      $forecasticons = $headers[1]; 

      // Now get just the bottom of the NWS page for editing 
      preg_match('|</table>(.*)<hr>|s', $html, $betweenspan); 
      $forecast  = $betweenspan[1]; 

      // Chop up each title text and place in array 
      preg_match_all('|<b>(.*): </b>|Ui', $forecast, $headers); 
      $forecasttitles = $headers[1]; 

      // Chop up each forecast text and place in array 
      preg_match_all('|</b>(.*)
|Ui', $forecast, $headers); 
      $forecasttext = $headers[1]; 

      // Grab the Last Update date and time. 
      preg_match('|Last Update: (.*)|', $html, $betweenspan); 
      $lastupdate  = $betweenspan[1]; 

function forecastfetchUrlWithoutHanging($url,$cacheurl)
   {
   // Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
   $numberOfSeconds=4;    

   // 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)
       {

       $html = implode('', file($cacheurl));
       return($html);

       // You may wish to remove the following debugging line on a live Web site
       // print("<!-- Network error: $errstr ($errno) -->");
       }    // 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, 128);
           }    // end while

       fclose ($socketConnection);

       }    // end else

   return($xml);

   }    // end function


?>

Ken,

That code works, i.e., I get the icons and the forecast. But I also get this on the bottom of the page:



PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 222 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 232 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 222 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 232 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 222 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 232 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 222 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 232 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 222 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 232 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 222 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 232 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 222 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 232 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 222 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 232 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 222 PHP Notice: Undefined offset: 0 in c:\program files\webweaver\docs\WX\advforecast2.php on line 232 PHP Notice: Undefined offset: 13 in c:\program files\webweaver\docs\WX\advforecast2.php on line 429 PHP Notice: Undefined offset: 13 in c:\program files\webweaver\docs\WX\advforecast2.php on line 430 


My hope is that the issue is in fact temporary and will revert soon. But is there an easy fix for the scary gobbelygook? 8O

Thanks for your help by the way…

Adam

Ken,

I replaced the two lines…works wonderfully!! :smiley:

I appreciate the time and effort!

Michael
http://www.relayweather.com

Ken…

Sheer wizardry 8O

All fixed up here as well :slight_smile:

Thanks,
Bob

I wonder why i am getting that code returned on the bottom of my page. Makes me wonder if there is a setting in my PHP to turn off error reporting?

UPDATE: I edited my php.ini file to turn off error reporting. Now it works perfectly. I would like to echo jimmyb’s question, i.e., with this new code, will the script automatically revert to the specific point forecast and not the zone forecast? Or do we need to change the script back once NWS fixes their technical difficulties?

Ken, also, thanks again for this fix.

See if this helps with your PHP error reporting…
http://us2.php.net/error_reporting

-Bob

Bob, that was it. Thanks!

Good deal :slight_smile:

-Bob

Oohh… I shouldn’t do coding late at night anymore… #-o

So, in the light of day, a couple more things were added to address the issues…
First, the URL for the test page I was not the one I was working on (but is now)
http://saratoga-weather.org/advforecast2-testpage.php should work as a demo.

Second, I did a bit more (belt and suspenders) check on the page from the cache, and fetch the backup if the cache page is bogus.

That’s done by replacing

if (filemtime($cacheName) + 1800 > time()) { 
      $html = implode('', file($cacheName)); 
    } else { 
      $html = fetchUrlWithoutHanging($fileName,$cacheName); 
	  if (preg_match('/Temporary|Location:|defaulting to/Uis',$html)) {
	     $usingFile = "(Zone forecast)";
         $html = fetchUrlWithoutHanging($backupfileName,$cacheName); 
	  }  
      $fp = fopen($cacheName, "w"); 
      $write = fputs($fp, $html); 
      fclose($fp);  } 

with

if (filemtime($cacheName) + 1800 > time()) { 
      $html = implode('', file($cacheName)); 
	  if (preg_match('/Temporary|Location:|defaulting to/is',$html)) {
	     $usingFile = "(Zone forecast)";
         $html = fetchUrlWithoutHanging($backupfileName,$cacheName); 
	  }  
    } else { 
      $html = fetchUrlWithoutHanging($fileName,$cacheName); 
	  if (preg_match('/Temporary|Location:|defaulting to/Uis',$html)) {
	     $usingFile = "(Zone forecast)";
         $html = fetchUrlWithoutHanging($backupfileName,$cacheName); 
	  }  
      $fp = fopen($cacheName, "w"); 
      $write = fputs($fp, $html); 
      fclose($fp);  } 

Third, the Notice: errors were due to fewer days being returned to the Zone forecast than the regular forecast (while error_reporting is set to E_ALL). That can be fixed by changing:

	  for ($i=0;$i<=13;$i++) {
        print "<tr valign =\"top\" align=\"left\">\n";

to the more sensible

	  for ($i=0;$i<count($forecasttitles);$i++) {
        print "<tr valign =\"top\" align=\"left\">\n";

(or using ‘error_reporting(E_ALL ^ E_NOTICE);’ )

Best regards,
Ken