Wouldn’t it be great if while you were searching for your next vacation, the website that listing the resorts also had the current temperature of that resort on the page so you could see what to expect if you were there today. Something like that sounds great at first, but unfortunately it isn’t always sunny and 80 degrees 365 days of the year unfortunately. Never the less, it still may be nice to include the temperature on a website you manage, so here’s one way to do it…
Simple PHP Example: View on GitHub
The simplest way to get the weather using this API is using file_get_contents and passing, via GET, two parameters into the URL (your API key, and the location name). I’ve found that using ARC or PostMan Chrome extensions make working with an API much easier initially. You can then apply what you learn in those extensions to your programming and know what should be returned.
An API key can be obtained free of change from OpenWeatherMap’s website and is valid for up to 60 calls a minute. See their pricing page for more info on the stipulations. With some PHP magic, this could work wonders for a vacation or other website that needs to display the weather. From wind speed, to temperature, humidity and latitude/longitude of cities, this API is pretty useful. If you’re a developer, you can even build an open-source weatehr station and feed your own data into the OpenWeatherMap system so others can read from your weather station.
Note on Usage:
You wouldn’t want to do a new API fetch on every page load because obviously you could have more than 60 visits in a minute and the weather won’t change that often anyway. What you’d want to do is create a cronjob to run every minute, or every five minutes, to fetch the weather and store it locally in a database or other storage mechanism. The website would then read the value from the stored location on your own server, instead of having to go out to the web to grab the data. Alternatively you could store it as a key/value pair in Redis or Memecached for faster access for all your visitors.
Sample of Array Returned: