Weather Data with Weatherstack API using PHP

Weather is the state of atmosphere to describe the degree to which it is hot or cold, wet or dry, calm or stormy, clear or cloudy.

We always curious to know about the weather around us and try to check weather conditions on websites that’s provide weather related information.

So if you’re thinking about to develop your own website to display weather information or working on project to display weather data, then you’re here at the right place. In this tutorial, you will learn how to implement functionality to display weather information using Weatherstack API with PHP.

The Weatherstack API provides real-time historical weather information. You can easily use this API in your website to display accurate weather information. The Weatherstack API can be integrated using all major programming language.

Also, read:


We will cover this tutorial step by step to integrate Weatherstack API using PHP with live example.

Step1: Get Weatherstack API Access Key

First we need to register to weatherstack API to create account to get API Access Key.

We will use API Access Key to create search query with search location to make HTTP API request to Weatherstack API get weather response JSON data.

https://api.weatherstack.com/current
?access_key = YOUR_ACCESS_KEY
&query = New York

Step2: Create Location Search Query

As we will integrate API to get data using PHP, so we will build search query using API Access Key and search location.

<?php 
$searchLocation = 'New York';

$searchQuery = http_build_query([
  'access_key' => 'API_ACCESS_KEY',
  'query' => $searchLocation,
]);
?>

Step3: Make HTTP API Request to Weatherstack API

We will HTTP request to Weatherstack API using search query to get weather response data into JSON format.


<?php 
$httpRequest = curl_init(sprintf('%s?%s', 'https://api.weatherstack.com/current', $searchQuery));
curl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, true);

$responseData = curl_exec($httpRequest);
curl_close($httpRequest);
?>

Step4: Display Weather Details

We will use response JSON data from Weatherstack API and decode JSON data to display weather information. We will store final result data array into a variable and then display weather information. Here we will display current weather information.

<?php
$result = json_decode($responseData, true);

echo "Current temperature in $searchLocation is {$result['current']['temperature']}℃", PHP_EOL;

echo "Wind speed in $searchLocation is {$result['current']['wind_speed']}℃", PHP_EOL;

echo "Humidity in $searchLocation is {$result['current']['humidity']}℃", PHP_EOL;

?>

Step5: Complete Code To Integrate Weatherstack API with PHP

Here is the complete code to integrate Weatherstack API with PHP to get real-time weather information.

<?php
$searchLocation = 'New York';

$searchQuery = http_build_query([
  'access_key' => 'API_ACCESS_KEY',
  'query' => $searchLocation,
]);

$httpRequest = curl_init(sprintf('%s?%s', 'https://api.weatherstack.com/current', $searchQuery));
curl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, true);

$responseData = curl_exec($httpRequest);
curl_close($httpRequest);

$result = json_decode($responseData, true);

echo "Current temperature in $searchLocation is {$result['current']['temperature']}℃", PHP_EOL;
?>

Below is the real-time response weather data in JSON format for the city of New York from Weatherstack API

{
    "request": {
        "type": "City",
        "query": "New York, United States of America",
        "language": "en",
        "unit": "m"
    },
    "location": {
        "name": "New York",
        "country": "United States of America",
        "region": "New York",
        "lat": "40.714",
        "lon": "-74.006",
        "timezone_id": "America/New_York",
        "localtime": "2019-11-07 08:14",
        "localtime_epoch": 1567844040,
        "utc_offset": "-4.0"
    },
    "current": {
        "observation_time": "12:14 PM",
        "temperature": 13,
        "weather_code": 113,
        "weather_icons": [
            "https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
        ],
        "weather_descriptions": [
            "Sunny"
        ],
        "wind_speed": 0,
        "wind_degree": 349,
        "wind_dir": "N",
        "pressure": 1010,
        "precip": 0,
        "humidity": 90,
        "cloudcover": 0,
        "feelslike": 13,
        "uv_index": 4,
        "visibility": 16
    }
}

Step6: Conclusion

In this tutorial we have explained how to get real-time weather information using Weatherstack API with PHP. You can also gone through documentation for more details and try advance options to integrate weather API into your application to get accurate weather information. You can also integrate the Weatherstack API with other major languages as well.

You may also like: