Weather is the most important for everyday life. Almost every day we have to check the weather report. We want to know the temperature report every day. If you need to show whether to your website for visitor's facility then you can add a real-time update by using AccuWeather API. Yes, this small tutorial is about How to get current temperature using AccuWeather API. First, you have to register for an AccuWeather API key. You will get it here, which package you want you will get it from AccuWeather Developer Page sign-up for an API code which package you want, you can try free package first. Once you registered they will give you an API key and you have to provide this key into your code for a valid API request.
Now, we are going to code. We'll extract temperature information from AccuWeather website using this code. We are going to create a function to get information Let's see the code-
<?php
function realtime_weather($location=28143){
$apiURL = 'http://apidev.accuweather.com/currentconditions/v1/'.$location.'.json?language=en&apikey=YOUR_KEY';
$content = file_get_contents($apiURL);
$jsonObject = json_decode($content);
return $jsonObject[0];
}
?>
We have created our function. Here is a parameter in realtime_weather() function and this parameter is for city code you will get your desire city code when you browse AccuWeather.
Search your location into the search box and you will get city code from the URL parameter, see the image. Now we are going to extract temperature from this function.
<?php
function realtime_weather($location=28143){
$apiURL = 'http://apidev.accuweather.com/currentconditions/v1/'.$location.'.json?language=en&apikey=YOUR_KEY';
$content = file_get_contents($apiURL);
$jsonObject = json_decode($content);
return $jsonObject[0];
}
print_r(realtime_weather(28143)->Temperature->Metric->Value . '° ' . realtime_weather(28143)->Temperature->Metric->Unit);
?>
This code will show you temperature like 32.8° C isn't it easy enough? I hope you understand this code if you have any question you can comment and if you need any help you can contact me. Be happy!