PHP Classes

File: src/Services/Weather/OpenWeatherApi.php

Recommend this page to a friend!
  Classes of Joseluis Laso   PHP Telegram CLI Wrapper   src/Services/Weather/OpenWeatherApi.php   Download  
File: src/Services/Weather/OpenWeatherApi.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Telegram CLI Wrapper
Send messages and other commands to Telegram users
Author: By
Last change:
Date: 8 years ago
Size: 1,658 bytes
 

Contents

Class file image Download
<?php

namespace TelegramCliWrapper\Services\Weather;

use
TelegramCliWrapper\Config;

class
OpenWeatherApi
{
   
/** @var mixed */
   
protected $weatherInfo;

    public function
__construct()
    {
       
$cacheData = __DIR__ . '/../../../data/weather-cached-data.json';

        if (!
file_exists($cacheData) || (filemtime($cacheData) < (intval(date("U")) - 15*60*60))) {
           
$config = Config::getInstance()->get('weather');
           
$city = $config['city'];
           
$country = $config['country'];
           
$appid = $config['appid'];
           
$info = file_get_contents("http://api.openweathermap.org/data/2.5/weather?q={$city},{$country}&APPID={$appid}");
           
file_put_contents($cacheData, $info);
        }

       
$this->weatherInfo = json_decode(file_get_contents($cacheData), true);
    }

   
/**
     * @return array
     */
   
public function getWeatherInfo()
    {
        return
$this->weatherInfo;
    }

    public function
getWeatherInfoAsString()
    {
        if (!
is_array($this->weatherInfo)){
            return
"Seems that openWeathermap is not configured correctly";
        }

        return
sprintf(
           
"In %s (%s) weather is %s - %s\nWind speed is %s.\nTemperature right now is %dÂșC\nHumidity is %s",
           
$this->weatherInfo['name'],
           
$this->weatherInfo['sys']['country'],
           
$this->weatherInfo['weather'][0]['main'],
           
$this->weatherInfo['weather'][0]['description'],
           
$this->weatherInfo['wind']['speed'],
           
round($this->weatherInfo['main']['temp'] - 273, 0),
           
$this->weatherInfo['main']['humidity']
        );
    }


}