Weather API (OpenWeather)

This API is free for 1000 calls per day, then has costs for subsequent calls. So, would need to create different accounts per project to avoid reaching the quota quick. 

Create account

Generate API Key

Weather

Sample Codes:

				
					<?php
$weather = get_transient( 'ff_weather' );
$location = 'Port Moresby';
if( !$weather ) {
    $api_key = 'ENTER_API_KEY_HERE';
    $data = file_get_contents_curl('https://api.weatherapi.com/v1/current.json?key='. $api_key .'&q='. urlencode($location));
    if( $data ) {
        $weather = json_decode($data);
        set_transient('ff_weather', $weather, 3600); // refresh once every hour
    }
}

if( !$weather ) return;
$temp = $weather->current->temp_c;
$condition = strtolower($weather->current->condition->text);

$type = 'cloudy';
if( strpos( $condition, 'rain' ) ) {
    $type = 'rainy';
} else if( $condition == 'sunny' ) {
    $type = 'sunny';
}

echo '<div class="weather cond_'. $type .'">';
    echo '<span class="location">'. $location .'</span>';
    echo '<span class="temp">'. $temp .'</span>';
echo '</div>';
				
			

Share This

Select your desired option below to share a direct link to this page.
Your friends or family will thank you later.