IT_Architect
asked on
PHP code to retrieve NDFD weather forecasts.
What I need is the PHP code to retrieve NDFD weather forecasts. I find examples, but most with broken links that don't actually work or throw errors. I'm using PHP 5.3 on a FreeBSD server.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fabulous!
See if this helps: https://iconoun.com/demo/temp_it_architect.php
https://www.experts-exchange.com/questions/28982716/How-can-I-assign-SimpleXMLElements-to-variables.html?anchorAnswerId=41885047#a41885047
<?php // demo/temp_it_architect.php
/**
* https://www.experts-exchange.com/questions/28982054/PHP-code-to-retrieve-NDFD-weather-forecasts.html
*
* https://www.experts-exchange.com/articles/12239/Introduction-to-Application-Programming-Interfaces.html
*/
error_reporting(E_ALL);
$url = <<<EOD
http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?whichClient=NDFDgenByDay&lat=38.99&lon=-77.01&listLatLon=&lat1=&lon1=&lat2=&lon2=&resolutionSub=&endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=¢erPointLat=¢erPointLon=&distanceLat=&distanceLon=&resolutionSquare=&zipCodeList=&citiesLevel=&format=24+hourly&startDate=2016-11-09&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>';
// ACTIVATE THIS TO SEE THE ENTIRE OBJECT
// var_dump($obj);
// SHOW HOW TO ACCESS SOME OF THE PROPERTIES
$t = $obj->head->product->title;
echo PHP_EOL . "TITLE: $t";
$lat = (string)$obj->data->location->point->attributes()->latitude;
$lon = (string)$obj->data->location->point->attributes()->longitude;
$geo = "$lat,$lon";
echo PHP_EOL . "GEOCODE: $geo";
$units = (string)$obj->data->parameters->temperature[0]->attributes()->units;
$name = (string)$obj->data->parameters->temperature[0]->name;
echo PHP_EOL . "$name ($units)";
$days = (array)$obj->data->{'time-layout'}[0]->{'start-valid-time'};
$values = (array)$obj->data->parameters->temperature[0]->value;
$icons = (array)$obj->data->parameters->{'conditions-icon'}->{'icon-link'};
foreach ($days as $key => $day)
{
$dow = date('D', strtotime($day));
$tmp = $values[$key];
echo PHP_EOL . "$dow $tmp°" . substr($units,0,1);
echo '<img src="' . $icons[$key] . '" />';
}
Cross-reference: https://www.experts-exchange.com/questions/28982716/How-can-I-assign-SimpleXMLElements-to-variables.html?anchorAnswerId=41885047#a41885047
ASKER
All I can say is WOW!
The NOAA information I was using disappeared. I also had a source for bulk information, and that is missing now too. Now I need to resource that information. You can do in minutes what takes me days. Should I start a new thread for what I need, or maybe one for each table that I need or advise if possible?
The NOAA information I was using disappeared. I also had a source for bulk information, and that is missing now too. Now I need to resource that information. You can do in minutes what takes me days. Should I start a new thread for what I need, or maybe one for each table that I need or advise if possible?
Sure - start a new thread. As long as it shows up in the PHP Zone, I'll see it, and I'll be glad to help.
All the best, ~Ray
All the best, ~Ray
ASKER
From what I see so far, it looks like you "hit the nail on the head" with that code. I could not get their code to work for some reason. All I found was broken links.
PS: What I need now is a good way to parse it. If you have some pointers on the best way to post that thread, let me know here.