Skip to main content

Build Weather Website with Weatherstack API using PHP

Weather is the information of atmosphere to describe the degree of a place. Weather information consists of detail about a location such as hot or cold, wet or dry, calm or stormy, clear or cloudy. The Weather information also includes weather forecasting to predict the conditions of atmosphere. The weather information are collected by Meteorological department.

There are many websites that displaying Weather information and we always checking these websites for current weather updates. But most of weather websites are slow and we started thinking to develop own website to display Weather information.

So if you’re thinking about developing your own Weather website then you’re here at the right place. In this tutorial, you will learn how to build your own Weather website using Weatherstack API using PHP.

Also, read:

We will implement to display real-time Weather information using Weatherstack API with PHP.

So let’s proceed to integrate Weatherstack API using PHP.

Step1: Get Weatherstack API Access Key

To integrate Weatherstack API to get realtime weather information, you need to sign up to Weatherstack API to get API Access Key.

We will use API Access Key to create search query to make HTTP request to API.

https://api.weatherstack.com/current
    ? access_key = YOUR_ACCESS_KEY

Step2: Create Weather Search Query

We will create search query using API access key and location to make HTTP request to get weather information using Weatherstack API.

<?php 
$keyword = 'San Francisco';

$searchQuery = http_build_query([
  'access_key' => 'YOUR_ACCESS_KEY',
  'query' => $keyword,
]);

?>

Step3: Make HTTP Request to Weatherstack API

We will HTTP request to Weatherstack API to using search query to get real-time weather information. The weather response information will be stored to a variable $response in JSON data format.

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

$response = curl_exec($ch);
curl_close($ch);
?>

Step4: Decode Response Data and Display Weather Information

We will decode the response JSON data and display weather information.

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

echo "Temperature in $keyword is {$result['current']['temperature']}℃", PHP_EOL;
echo "The wind speed in $keyword is {$result['current']['wind_speed']}℃", PHP_EOL;
echo "The visibility in $keyword is {$result['current']['visibility']}℃", PHP_EOL;
?>

Step5: Complete Code to get Weather Data using Weatherstack API

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

<?php 

$keyword = 'London';

$searchQuery = http_build_query([
  'access_key' => 'YOUR_ACCESS_KEY',
  'query' => $keyword,
]);

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

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);

echo "Temperature in $keyword is {$result['current']['temperature']}℃", PHP_EOL;
echo "The wind speed in $keyword is {$result['current']['wind_speed']}℃", PHP_EOL;
echo "The visibility in $keyword is {$result['current']['visibility']}℃", PHP_EOL;
?>

Below is the Weather response data from Weatherstack API in JSON format for the “San Francisco” USA.

{
    "request": {
        "type": "City",
        "query": "San Francisco, United States of America",
        "language": "en",
        "unit": "m"
    },
    "location": {
        "name": "San Francisco",
        "country": "United States of America",
        "region": "California",
        "lat": "37.775",
        "lon": "-122.418",
        "timezone_id": "America/Los_Angeles",
        "localtime": "2019-09-03 05:35",
        "localtime_epoch": 1567488900,
        "utc_offset": "-7.0"
    },
    "current": {
        "observation_time": "12:35 PM",
        "temperature": 16,
        "weather_code": 122,
        "weather_icons": [
            "https://assets.weatherstack.com/images/symbol.png"
        ],
        "weather_descriptions": [
            "Overcast"
        ],
    "wind_speed": 17,
    "wind_degree": 260,
    "wind_dir": "W",
    "pressure": 1016,
    "precip": 0,
    "humidity": 87,
    "cloudcover": 100,
    "feelslike": 16,
    "uv_index": 0,
    "visibility": 16
    }
}

Step6: Conclusion

In this tutorial you have learned how to get real-time weather information using Weatherstack API with PHP. You can also
check documentation for more advance options to integrate API into your application to get real-time weather information. The Weatherstack API can also be integrated with other major languages as well.

You may also like: