Local Storm Reports script

Many years ago, Curly created a script to display the LSR products from offices. Lately, it hasn’t been working for me. I’m somewhat familiar with PHP, but I can’t figure out what the issue is. Below is the first 77 lines of the script. The whole script is too large to post.

If someone can figure out the issue (Ken???) I’d be very grateful.

<?php
//////////////////////////////////////////////////////////////////// 
//                                                                //
//                 Local Storm Report from NOAA                   //
//                                                                //
//                   Last update    7/01/2008                     //
//                                                                //
//                                                                //
//               If fopen is disabled on your server,             //
//                  page format errors will occur.                //
//                 A revised script is available at               //
//  http://www.weather.ricksturf.com/scripts/NOAAstormreport.zip  //
//                  This is free to use and modify.               //
//                                                                //
//                       [email protected]                      //
//                                                                //
////////////////////////////////////////////////////////////////////

// Enter your NOAA code.  This is the only thing you need to change.
$noaa_id = "btv";  // Three letter code for YOUR nearest NOAA center

///////////////////////////////////////////////////////////////////////////////

// Start on page one
$noaa_page = "1";

//  Sets the page to start for your NOAA location
preg_match('/noaa_id2/', $_SERVER['REQUEST_URI'], $matchnoaa_id2);

  if ($matchnoaa_id2[0] != "noaa_id2") {
    $noaa_id2 = $noaa_id;
   }

// Getss noaa_id2=3 letter location from url
preg_match("|noaa_id2=(.*)|is", $_SERVER['REQUEST_URI'], $getnoaa_id2);

  if ($matchnoaa_id2[0] == "noaa_id2") {
    $noaa_id2 = $getnoaa_id2[1];
   }

preg_match("|issuer(.*)page|Uis", $_SERVER['REQUEST_URI'], $issuer);
preg_match("|[A-Z]page(.*)|s", $_SERVER['REQUEST_URI'], $page_num);
preg_match('/noaapage/', $_SERVER['REQUEST_URI'], $noaapage);

  if ($noaapage[0] == "noaapage") {
    $noaa_id2 = $issuer[1];  //  Three letter code for NOAA
    $noaa_page = $page_num[1]; //  Page, or version, number
   }

// Location of the LSR
$noaasr  = "http://forecast.weather.gov/product.php?site=".$noaa_id."&issuedby=".$noaa_id2."&product=LSR&version=" . $noaa_page;
$amp = "/&/ ";
$amper = '&amp;';
$noaasrA = preg_replace($amp, $amper, $noaasr);
$year = date("Y");
$html = implode('', file($noaasr));

// Adds && if it is missing for formatting
$html = preg_replace("/([A-Z0-9])(.)(\n)(\n)([$])(.*)/", '${1}${2}${3}${4}${4}&&', $html);

// Adds && if it is missing for formatting
$html = preg_replace("/([A-Z0-9])(\n)(\n)([$])(.*)/", '${1}${2}${3}${3}&&', $html);

// Removes year in the date if it is in a remark
$html = preg_replace("/(\/)($year)(.*)(\/)($year)/", '${3}', $html);

//  Fix for 4 extra lines
$html = preg_replace("/([A-Z])(.*)(\n)(\n)(\n)(\n)(&&)/", '${1}${2}${3}${4}${6}${7}', $html);

//  Fix for extra line
$html = preg_replace("/(\n)( \s\s+)([A-Z])(.*)(\n)(\n)( \s\s+)/", '${1}${2}${3}${4}${5}${7}', $html);

//  Finds if no reports are listed
preg_match_all('|<span style="color:Red;">(.*)</span></div>|Uis', $html, $noissue);  //  gets the noissue from NOAA

$nonereported = $noissue[1][0];
$noreport = "None issued by this office recently.";

Sure… try changing

$year = date("Y");
$html = implode('', file($noaasr));

to

$year = date("Y");
  $STRopts = array(
	  'http'=>array(
	  'method'=>"GET",
	  'protocol_version' => 1.1,
	  'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
				"Cache-control: max-age=0\r\n" .
				"Connection: close\r\n" .
				"User-agent: Mozilla/5.0 (NOAAstormreport, weather.ricksturf.com)\r\n" .
				"Accept: text/plain,text/html\r\n"
	  ),
	  'https'=>array(
	  'method'=>"GET",
	  'protocol_version' => 1.1,
	  'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
				"Cache-control: max-age=0\r\n" .
				"Connection: close\r\n" .
				"User-agent: Mozilla/5.0 (NOAAstormreport, weather.ricksturf.com)\r\n" .
				"Accept: text/plain,text/html\r\n"
	  )
	);
	
   $STRcontext = stream_context_create($STRopts);

$html = implode('', file($noaasr,false, $STRcontext));

The NWS forced requests to have a User-agent: header string last year. That’s why the old script was failing (the default for a file() URL access is to have no User-agent defined).
Best regards,
Ken

Thanks Ken. That worked like a charm.

Well, it at least worked to get the file, but there’s a couple extra commas in the parsed data, so it’s won’t display it. I’m trying to sort through it, but not having much luck.