Initial Release
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/vendor/
|
19
composer.json
Normal file
19
composer.json
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "liberty/weatherman",
|
||||||
|
"description": "Package to get Windhoeks weather",
|
||||||
|
"type": "library",
|
||||||
|
"license": "MIT",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Liberty\\Weatherman\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Lee Sunde",
|
||||||
|
"email": "liberty@nampharm.com.na"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"minimum-stability": "stable",
|
||||||
|
"require": {}
|
||||||
|
}
|
18
composer.lock
generated
Normal file
18
composer.lock
generated
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"_readme": [
|
||||||
|
"This file locks the dependencies of your project to a known state",
|
||||||
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
|
"This file is @generated automatically"
|
||||||
|
],
|
||||||
|
"content-hash": "85b5ce5bd60a2174957bfd3e24a64f38",
|
||||||
|
"packages": [],
|
||||||
|
"packages-dev": [],
|
||||||
|
"aliases": [],
|
||||||
|
"minimum-stability": "stable",
|
||||||
|
"stability-flags": {},
|
||||||
|
"prefer-stable": false,
|
||||||
|
"prefer-lowest": false,
|
||||||
|
"platform": {},
|
||||||
|
"platform-dev": {},
|
||||||
|
"plugin-api-version": "2.6.0"
|
||||||
|
}
|
34
src/getWeather.php
Normal file
34
src/getWeather.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class getWeather
|
||||||
|
{
|
||||||
|
private $apiUrl = 'https://wttr.in/Windhoek?format=j1';
|
||||||
|
|
||||||
|
public function getWeather()
|
||||||
|
{
|
||||||
|
$response = @file_get_contents($this->apiUrl);
|
||||||
|
if ($response === false) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$data = json_decode($response, true);
|
||||||
|
if (!$data || !isset($data['current_condition'][0])) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
'temperature_C' => $data['current_condition'][0]['temp_C'],
|
||||||
|
'weather_desc' => $data['current_condition'][0]['weatherDesc'][0]['value'],
|
||||||
|
'humidity' => $data['current_condition'][0]['humidity'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Example usage:
|
||||||
|
$weather = new getWeather();
|
||||||
|
$current = $weather->getWeather();
|
||||||
|
if ($current) {
|
||||||
|
echo "Temperature: {$current['temperature_C']}°C\n";
|
||||||
|
echo "Condition: {$current['weather_desc']}\n";
|
||||||
|
echo "Humidity: {$current['humidity']}%\n";
|
||||||
|
} else {
|
||||||
|
echo "Could not fetch weather data.\n";
|
||||||
|
}
|
Reference in New Issue
Block a user