So i've got a list with local weather details, http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl. And I want to display some of that in formation via php on my site, but can't really find out how JSON is something completely new for me.
And the only thing i managed to do right now is this: http://jeroenonline.biz/JSON/index.php. So this is a simple script:
$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");
$decode = json_decode($getData);
echo "<pre>";
print_r($decode);
The json_decode() function is used to decode or convert a JSON object to a PHP object.
To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.
The php function file_get_contents($url) send a http request to the provided url and returns json data. The function json_decode($json) decodes the provided json string and returns as a PHP object. As simple as that you can parse json response. That was all about getting json from url in php.
using the link
http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl
without the "." gives me a response
{
"message": "Error: Not found city",
"cod": "404"
}
<?php
$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");
$decode = json_decode($getData);
// accessing it through object
echo $decode->message;
echo "<br/>";
echo $decode->cod;
// accessit via array
// set true the second parameter or the json_decode($encoded_data, TRUE)
// to give you array
$decode = json_decode($getData, TRUE);
echo "<br/>";
echo $decode['message'];
echo "<br/>";
echo $decode['cod'];
so when use the link with the "."
http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl.
gives a response of :
{
"coord": {
"lon": 5.83,
"lat": 50.91
},
"sys": {
"message": 0.0287,
"country": "Netherlands",
"sunrise": 1430884846,
"sunset": 1430939149
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "Sky is Clear",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 284.923,
"temp_min": 284.923,
"temp_max": 284.923,
"pressure": 1012.18,
"sea_level": 1023.56,
"grnd_level": 1012.18,
"humidity": 67
},
"wind": {
"speed": 6.06,
"deg": 219.002
},
"clouds": {
"all": 0
},
"dt": 1430875602,
"id": 0,
"name": "Nuth",
"cod": 200
}
to show the the result
// sample to access coord
echo $decode->coord->lon;
echo $decode->coord->lat;
// sample to access sys
echo $decode->sys->message;
echo $decode->sys->country;
// sample to access weather
echo $decode->weather[0]->id;
echo $decode->weather[0]->main;
echo $decode->weather[0]->description;
// sample to access main
echo $decode->main->temp;
echo $decode->main->temp_min;
// sample to access wind
echo $decode->wind->speed;
// sample to access clouds
echo $decode->clouds->all;
echo $decode->id;
echo $decode->name;
echo $decode->cod;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With