If you want to test API in PHP, here is a useful free Current weather API.
https://openweathermap.org/api
Contents
First you have to SignuUp for FREE API key.
https://home.openweathermap.org/users/sign_up
Create and view Current weather API key:
https://home.openweathermap.org/api_keys
Call current weather data
How to make an API call
Access current weather data for any location on Earth! We collect and process weather data from different sources such as global and local weather models, satellites, radars and a vast network of weather stations. Data is available in JSON, XML, or HTML format.
Examples of API calls
https://api.openweathermap.org/data/2.5/weather?lat=44.34&lon=10.99&appid={API key}
Simple use in PHP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php // TOKEN - API KLJUČ $apiKey="affXXXXXXXXXXXXXXXXXXXXX"; $city="London"; // GRAD ZA KOJI DOHVAĆAMO PODATKE $url2="https://api.openweathermap.org/data/2.5/weather?q=$city&appid=$apiKey"; // API POZIV PREPISAN SA STRANICE $odgovor=file_get_contents($url2); var_dump($odgovor); $data=json_decode($odgovor); var_dump($data); //provjera odgovora - jesmo li ih dohvatili if($data->cod === 200) { $temp=$data->main->temp; //pazimo na dohvat podataka prema dokumentaciji var_dump($temp); $opis=$data->weather[0]->description; var_dump($opis); echo "Temperatura u $city je: $temp."; echo "Vremenski uvjeti su: $opis."; } else { echo "Došlo je do pogreške prilikom dohvata podataka"; } ?> |