Author Topic: How can I get the wordedForecast text XML?  (Read 821 times)

0 Members and 1 Guest are viewing this topic.

Offline IT_Architect

  • Member
  • *
  • Posts: 3
How can I get the wordedForecast text XML?
« on: November 15, 2016, 01:25:18 PM »
I'm storing Weather.gov information in a database.  I've gotten pretty much everything I need for Currents in XML by ICAO, Warnings and Advisories in XML for the US, and Forecast XML using REST.  What I haven't been able to find is a way to pull in wordedForecast in XML like you see on the Weather.gov pages.  Does anyone have links and/or code for pulling in the wordedForecast information?

Thanks!

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: How can I get the wordedForecast text XML?
« Reply #1 on: November 15, 2016, 03:55:00 PM »
Have you tried something like this:

http://forecast.weather.gov/MapClick.php?lat=37.2725&lon=-122.0271&unit=0&lg=english&FcstType=dwml

It's available via the XML button under the map on a point forecast page.

The new design they're working on has a XML button also, but it seems to not be working the same... the V3 site of theirs is still in beta, so it may be fixed more as time goes on.
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 IT_Architect

  • Member
  • *
  • Posts: 3
Re: How can I get the wordedForecast text XML?
« Reply #2 on: November 15, 2016, 04:04:54 PM »
I discovered that only an hour ago or so.  I couldn't get the text forecast from REST.  It works fine here.  My problem is the php code to access the information now.  With REST, this works fine:
Code: [Select]
#!/usr/local/bin/php
<?php
error_reporting
(E_ALL);

$url = <<<EOD
http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?whichClient=NDFDgenByDay&lat=42.8808333&lon=-85.5228056&listLatLon=&lat1=&lon1=&lat2=&lon2=&resolutionSub=&endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=&centerPointLat=&centerPointLon=&distanceLat=&distanceLon=&resolutionSquare=&zipCodeList=&citiesLevel=&format=24+hourly&startDate=&numDays=7&Unit=e&Submit=Submit
EOD;

// SHOW HOW TO RETRIEVE DATA FROM THE API
$xml file_get_contents($url);
$obj SimpleXML_Load_String($xml);
echo 
'<pre>';
var_dump($obj);

However, when I substitute:
Code: [Select]
http://forecast.weather.gov/MapClick.php?lat=42.8808333&lon=-85.5228056&FcstType=dwml&Submit=Submit
It doesn't work.

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: How can I get the wordedForecast text XML?
« Reply #3 on: November 15, 2016, 04:11:13 PM »
The NWS implemented a requirement that all requests to forecast.weather.gov have a User-agent: header.  You'll need to add a bit to your code before the file_get_contents() to establish a stream context with the headers needed.  I use
Code: [Select]
$opts = 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: Forecast loader\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: Forecast loader\r\n" .
            "Accept: text/plain,text/html\r\n"
  )
);

$context = stream_context_create($opts);

then change the
Code: [Select]
$xml = file_get_contents($url); to
Code: [Select]
$xml = file_get_contents($url,false,$context); and the XML should be retrieved from forecast.weather.gov.  The new forecast-v3.weather.gov requires HTTPS only, and this fix will support either http:// or https://
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 IT_Architect

  • Member
  • *
  • Posts: 3
Re: How can I get the wordedForecast text XML?
« Reply #4 on: November 15, 2016, 06:54:59 PM »
Worked Perfectly!   =D&gt;
Awesome!!!  Thank you, thank you, thank you.