WordPress – How to get weather update without using plugins
Generally we have planned to use Google api to get the weather updates exactly. But working with Google api is not simple like openweathermap. So let’s try to get current weather of London with the following code.
$lndn_request = 'http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0';
The above one will help us to get an json encoded string from openweathermap. It’s absolutely free to get. But you need an api to retrieve it from server. There before they didn’t ask api key for it. So use the above key. Which is demo one.
So, We have to decode the json string now
$lndn_response = file_get_contents($lndn_request); $lndn_jsonobj = json_decode($lndn_response);
Now, the decoded string is an object. So let’s get the temperature alone to use it on our site. But the temperature is in Kelvin.
$lndn_Now_temp = $lndn_jsonobj->main->temp;
Now the Kelvin temperature in our variable. We need to convert it into Celsius. Let’s use the follwoing function to get it in Celsius.
$lndn_temp = round(($lndn_Now_temp - 273.15));
You can use the above `$lndn_temp` variable to echo the London weather. Now we can echo it like this.
echo $lndn_temp. ‘ ° C’;