Coders Packet

PHP Class for OpenWeatherMap API

By Faruque Ahamed Mollick

This is a PHP class for OpenWeatherMap API. OpenWeatherMap provides current weather and weather forecasting data.

Using this PHP class, you can quickly work with the OpenWeatherMap API. You can fetch the current weather data and weather forecast data.

 

Fetch current weather data

First, include the classes/CurrentClass.php file. After that, create an object of the class. The OpenWeatherMap API key, city and the unit should be pass during creating the class as you can see below:

$currentObj = new CurrentClass("OpenWeatherMap_API_KEY", "CITY", "UNIT");

The $currentObj variable/array contains all the current weather data as an array.

For example, you can print the current temperature from the $currentObj array just like you can see below in PHP:

echo $currentObj->temp();

 

Fetch weather forecasting data

For getting the weather forecasting data, you have to include the classes/Forecast.php file. Below is how to do it:

$forecast_Obj = new Forecast("OpenWeatherMap_API_KEY", "CITY", "UNIT");

The $forecast_Obj is an array and it contains all the forecasting data.

Below is the example of PHP code where it is given how to get the maximum temperatures for the upcoming 7 days:

<?php
  for ($i=0; $i <= 6 ; $i++) {
   echo $forecast_Obj->maxTemp($i).', ';
  }
?>

Visit our GitHub page for this project

 

Download Complete Code

Comments

No comments yet