Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP code to retrieve NDFD weather forecasts.

Posted on 2016-11-09
6
Medium Priority
?
88 Views
Last Modified: 2016-11-14
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.
0
Comment
Question by:IT_Architect
  • 3
  • 3
6 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 41880867
Is this what you're looking at?
http://www.nws.noaa.gov/ndfd/

If so, it looks like they have a RESTful API.
http://graphical.weather.gov/xml/rest.php

Here's an example showing what you get back from the API
https://iconoun.com/demo/temp_it_architect.php
<?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=&centerPointLat=&centerPointLon=&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>';
var_dump($obj);

Open in new window

This article has some general information about APIs.
https://www.experts-exchange.com/articles/12239/Introduction-to-Application-Programming-Interfaces.html
0
 

Author Comment

by:IT_Architect
ID: 41883911
Hi Ray,
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.
0
 

Author Closing Comment

by:IT_Architect
ID: 41883914
Fabulous!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41883992
See if this helps: https://iconoun.com/demo/temp_it_architect.php
<?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=&centerPointLat=&centerPointLon=&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&deg;" . substr($units,0,1);
    echo '<img src="' . $icons[$key] . '" />';
}

Open in new window

Cross-reference:
https://www.experts-exchange.com/questions/28982716/How-can-I-assign-SimpleXMLElements-to-variables.html#a41885047
0
 

Author Comment

by:IT_Architect
ID: 41886354
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?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41886371
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question