Skip to content

Instantly share code, notes, and snippets.

@danielme85
Last active August 1, 2022 16:34
Show Gist options
  • Save danielme85/8ddd04431ea2d6982e0b14802464ea0a to your computer and use it in GitHub Desktop.
Save danielme85/8ddd04431ea2d6982e0b14802464ea0a to your computer and use it in GitHub Desktop.
Pulls weather data from openweathermap using Laravel 5.* framework, based on cords from GeoIP2: https://github.com/danielme85/laravel-geoip2
<?php
namespace App\Addons;
use GuzzleHttp;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Log;
class Weather
{
protected $settings;
/**
* Weather constructor.
* @array $settings
*/
function __construct($settings = array())
{
if (!empty($settings)) {
$this->settings = $settings;
} else {
//$this->settings = config('weather'); <- or make config/weaether.php file
$this->settings = [
/*
|--------------------------------------------------------------------------
| Weather service
|--------------------------------------------------------------------------
| config/weaether.php
*/
//default provider
'defaultProvider' => 'openWeatherMap',
'useSession' => true,
'sessionTimeout' => 30,
/*
|--------------------------------------------------------------------------
| OpenWeatherMap
|--------------------------------------------------------------------------
|
| http://openweathermap.org/api
|
*/
'openWeatherMap' => [
'appid' => '', //Your app id
'units' => 'imperial', // standard/metric/imperial units are available.
'lang' => 'en' //English - en, Russian - ru, Italian - it, Spanish - es (or sp), Ukrainian - uk (or ua), German - de, Portuguese - pt, Romanian - ro, Polish - pl, Finnish - fi, Dutch - nl, French - fr, Bulgarian - bg, Swedish - sv (or se), Chinese Traditional - zh_tw, Chinese Simplified - zh (or zh_cn), Turkish - tr, Croatian - hr, Catalan - ca
]
];
}
if (empty($this->settings)) {
abort(500, 'No weather settings found!');
}
}
/**
* Static wrapper, getWeatherByCord
* @param $long
* @param $lat
* @param $provider //specify a weather provider
* @param bool $refresh //force a refresh if using session
* @return null|string
*/
public static function weatherByCord($long, $lat, $provider = null, $refresh = false)
{
$weather = new self();
return $weather->getWeatherByCord($long, $lat, $provider, $refresh);
}
/**
* Get weather by coordinates.
*
* @param $long
* @param $lat
* @param $provider //specify a weather provider
* @param bool $refresh //force a refresh if using session
* @return null|string
*/
function getWeatherByCord($long, $lat, $provider = null, $refresh = false)
{
$weather = array();
if (!$provider) {
$provider = $this->settings['defaultProvider'];
}
if ($this->settings['useSession']) {
//Check if already present in session.
if ($weathersession = Session::get('weather') and !$refresh) {
$weather = json_decode($weathersession, true);
}
}
if (empty($weather) and $long and $lat) {
if ($provider === 'openWeatherMap') {
if (!$openweatherconfig = $this->settings[$provider]) {
Log::error('Could not load weather config...');
}
//Get config values
$appid = $openweatherconfig['appid'];
$units = $openweatherconfig['units'];
$lang = $openweatherconfig['lang'];
$url = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=$lat&lon=$long&units=$units&mode=json&lang=$lang&appid=$appid";
$json = $this->sendRequest($url);
if (!empty($json)) {
$process = json_decode($json, true);
//$weather['forecast'][$provider] = $process;
if (!empty($process['list'])) {
$i = 0;
foreach ($process['list'] as $forecast) {
$forecast['date'] = date('M/D/Y', $forecast['dt']);
$forecast['day'] = date('l', $forecast['dt']);
$process['list'][$i] = $forecast;
$i++;
}
}
$weather['forecast'] = $process;
}
sleep(1);
$url = "http://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$long&units=$units&mode=json&lang=$lang&appid=$appid";
$json = $this->sendRequest($url);
if (!empty($json)) {
$process = json_decode($json, true);
if (!empty($json)) {
$process = json_decode($json, true);
$process['date'] = date('M/D/Y', $process['dt']);
$process['day'] = date('l', $process['dt']);
}
$weather['current'] = $process;
}
//Store in session
Session::put('weather', json_encode($weather), $this->settings['sessionTimeout']);
}
}
return $weather;
}
/**
*
* @param $url
* @return string
*/
private function sendRequest($url)
{
$guzzle = new GuzzleHttp\Client();
$response = $guzzle->request('GET', $url);
return $response->getBody()->getContents();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment