-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherAPI.php
More file actions
59 lines (54 loc) · 1.77 KB
/
WeatherAPI.php
File metadata and controls
59 lines (54 loc) · 1.77 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* @package Sebcodes
* @category Sebcodes Project
* @author Sebastian Kiefer (sebcodes)
* @version 1.0
* @copyright 2020 Sebastian Kiefer
* @since 2020
* @link https://sebcodes.de
**/
namespace Sebcodes;
use Exception;
use stdClass;
class WeatherApi
{
private string $apikey;
private string $countyTown;
private object $weather;
public function __construct($countyTown)
{
if (empty($countyTown)) throw new Exception("Town required");
$this->countyTown = $countyTown;
$this->apikey = "your_api_key";
}
public function get()
{
$weather = new stdClass;
try {
$jsonfile = @file_get_contents("http://api.openweathermap.org/data/2.5/weather?q=".$this->countyTown."&units=metric&appid=".$this->apikey."");
$this->jsondata = json_decode($jsonfile);
if(!empty($this->jsondata)){
$weather->temp = $this->jsondata->main->temp;
$weather->pressure = $this->jsondata->main->pressure;
$weather->mintemp = $this->jsondata->main->temp_min;
$weather->maxtemp = $this->jsondata->main->temp_max;
$weather->wind = $this->jsondata->wind->speed;
$weather->humidity = $this->jsondata->main->humidity;
$weather->desc = $this->jsondata->weather[0]->description;
$weather->maind = $this->jsondata->weather[0]->main;
$weather->countyTown = $this->countyTown;
return $weather;
}
#set temperature to zero
else{
$weather->temp = 0;
return $weather;
}
}
catch (Exception $ex) {
$weather->temp = 0;
return $weather;
}
}
}