Author Topic: get-metar-conditions-inc.php fix when using NWS API  (Read 468 times)

0 Members and 1 Guest are viewing this topic.

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 949
    • LexMAWeather
get-metar-conditions-inc.php fix when using NWS API
« on: July 15, 2020, 10:50:26 AM »
Hi Ken,

Apparently, some time after you added the NWS API option to the METAR code, the NWS changed the return headers to and the code detects a successful fetch as a fail (because only "200" is returned and not "200 OK").

Code: [Select]
HTTP/2 200
server: nginx/1.16.1
content-type: application/geo+json
last-modified: Wed, 15 Jul 2020 14:09:00 GMT
access-control-allow-origin: *
access-control-allow-headers: Feature-Flags

etc...

It's just a case of stale code. I pulled over the header processing code from advforecast2 and it works for both the API and the NOAA site.

Code: [Select]
/*
      $i = strpos($rawhtml,"\r\n\r\n");
      $headers = substr($rawhtml,0,$i-1);
      $content = substr($rawhtml,$i+2);
      $RC = '';
      if (preg_match("|^HTTP\/\S+ (.*)\r\n|",$rawhtml,$matches)) {
$RC = trim($matches[1]);
      }
 */

      $stuff = explode("\r\n\r\n",$rawhtml); // maybe we have more than one header due to redirects.
      $content = (string)array_pop($stuff); // last one is the content
      $headers = (string)array_pop($stuff); // next-to-last-one is the headers
      preg_match('/HTTP\/\S+ (\d+)/', $headers, $m);
      if(isset($m[1]))
        $RC = (string)$m[1];
      else
        $RC = '0';

      // if(!preg_match('|200 |',$RC))
      if ($RC != "200")
      {
https://lexmaweather.info
On Mastodon: @LexMAWeather@toot.community

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: get-metar-conditions-inc.php fix when using NWS API
« Reply #1 on: July 15, 2020, 12:58:28 PM »
Was this issue with the Version 1.18 - 07-Nov-2019 get-metar-conditions-inc.php ?

The code there
Code: [Select]
      $rawhtml = mtr_fetchUrlWithoutHanging($metarURL);
      $i = strpos($rawhtml, "\r\n\r\n");
      $headers = substr($rawhtml, 0, $i - 1);
      $content = substr($rawhtml, $i + 2);
      $RC = '';
      if (preg_match("|^HTTP\/\S+ (.*)\r\n|", $rawhtml, $matches)) {
        $RC = trim($matches[1]);
      }

      if (!preg_match('|200|', $RC)) {
        $t = array(
          "unable to load $icao data RC=$RC",
          5,
          'day_partly_cloudy.gif',
          "unable to load $icao data RC=$RC",
          array() ,
          time()
        );
        $Debug.= "<!-- mtr_conditions returns RC='" . $RC . "' for ICAO/METAR='$icao' -->\n";
        return $t;
      }
should have handled the HTTP/1.1 or HTTP/2 responses for '200' or '200 OK' equally well.  Was it getting a redirect on the API query??
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 949
    • LexMAWeather
Re: get-metar-conditions-inc.php fix when using NWS API
« Reply #2 on: July 15, 2020, 02:05:23 PM »
Ooof. Merge error on my part. I somehow got a space after the "200" in the preg parameter.

Carry on.
https://lexmaweather.info
On Mastodon: @LexMAWeather@toot.community

 

anything