ajur-media/ajur.weather

AJUR Media 汽艇气象工具包

1.99.1 2024-08-23 14:11 UTC

This package is not auto-updated.

Last update: 2024-09-20 14:35:21 UTC


README

方法

Weather::init()

/**
 * @param null $api_key -- API-ключ для доступа к OpenWeather
 * @param array $options -- 2 опции: units:metric|imperial, lang:ru|en|..
 * @param null $logger -- null или Logger
 * @throws Exception
 */
public static function init($api_key = null, $options = [], $logger = null)

要从 OpenWeatherMap 服务器加载天气,需要指定 API-key。要从文件加载天气,只需(但不是必须)指定 Logger。

Weather::loadLocalWeather()

/**
 * @param int $district_id
 * @param null $source_file
 * @return array
 */
public static function loadLocalWeather($district_id = 0, $source_file = null)

从 JSON 文件中加载特定区域(0 为所有区域)的天气数据。文件结构

{
    "update_ts": 1592480232,
    "update_time": "2020-06-18 14-37-12",
    "data": {}
}

data - 对象,其中键为 OpenWeatherMap 中的区域标识符,键的数据为该区域的天气信息。

Weather::fetchWeatherGroup()

/**
 * Загружает из OpenWeather погоду для переданного массива регионов
 *
 * @param array $regions_list - либо список регионов (многомерный массив), либо список ключей регионов (array_keys)
 * @return array
 * @throws OpenWeatherMap\Exception
 */
public static function fetchWeatherGroup(array $regions_list):array

$regions_list 参数可以是区域列表(多维数组类型 WeatherConstants::outer_regions),也可以是 OpenWeatherMap 格式的区域标识符列表(通常为 array_keys(WeatherConstants::outer_regions)

Weather::fetchWeatherGroupDebug()

/**
 * @param array $regions_list
 * @return array
 * @throws OpenWeatherMap\Exception
 */
public static function fetchWeatherGroupDebug(array $regions_list): array

调试方法。参数只能是多维数组列表。

常量(WeatherConstants)

区域标识符表示在两个值域中。

  • 47news 网站的内码(<100)
  • OpenWeatherMap 的代码

常量

  • lo_adjacency_lists - 列表邻接区域列表。值是 47news 网站上的区域内部标识符
  • map_intid_to_owmid - 将列宁格勒州的区域映射到 OWMID 区域的表
  • REGION_SPB - OWMID 空间中“圣彼得堡”区域的代码
  • REGION_LO - OWMID 空间中“列宁格勒州”区域的代码
  • outer_regions - OWMID 空间中所有圣彼得堡和列宁格勒州的区域列表
  • icons_conversion - 映射表 - 天气代码到图标文件名

如何使用?

从 OWM 服务器获取天气

require_once 'vendor/autoload.php';

use AJUR\Toolkit\Weather;

Weather::init('<api key>');

$data = Weather::fetchWeatherGroup( array_keys( \AJUR\Toolkit\WeatherConstants::outer_regions ) );

$data = [
    'update_ts'     =>  time(),
    'update_time'   =>  (new \DateTime())->format('Y-m-d H-i-s'),
    'data'          =>  $data
];

file_put_contents('weather.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE  ), LOCK_EX);

在网站上加载天气

use AJUR\Toolkit\Weather;

Weather::init(null, [], AppLogger::scope('log.weather'));
$weather_source = getenv('STORAGE.WEATHER');
$weather = Weather::loadLocalWeather($current_district_id, $weather_source);