pyrrah/openweathermap-bundle

从 OpenWeatherMap API 获取当前天气的简单方法,适用于 Symfony 5 到 6。

安装: 199

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 3

分支: 1

开放问题: 0

类型:symfony-bundle

v1.1.0 2022-09-12 13:21 UTC

This package is auto-updated.

Last update: 2024-09-21 19:07:11 UTC


README

Latest Version on Packagist Software License Total Contributors Total Downloads

此包允许您通过 OpenWeatherMap 服务轻松获取城市或位置的天气。

要求

²请注意,OpenWeatherMap 的免费版本限制了每月的调用次数。请参阅 文档)。对于大量的调用,我建议设置缓存(当前此包未管理)。

安装

  1. 要安装此包,运行以下 Composer 命令
composer require pyrrah/openweathermap-bundle
  1. 检查配置文件是否正确安装,并使用您的值编辑默认值(《官方 API 文档》)
  # config/packages/pyrrah_openweathermap.yaml
  pyrrah_open_weather_map:
      api_key: your_api_key
      api_url: https://api.openweathermap.org/data/2.5/
      units: metric
      language: en

路由(可选)

如果您不想通过应用程序公开 OpenWeatherMap API,则可以跳过本节。

配置

PyrrahOpenWeatherMapBundle:
    resource:		"@PyrrahOpenWeatherMapBundle/Controller/"
    type:		annotation
    prefix:		/openweathermap/api

这通过 /openweathermap/api 暴露了 OpenWeatherMap API。这意味着您现在可以发送一个未签名的请求到 /openweathermap/api/*,而不是向 http://api.openweathermap.org/ 发送请求。如果不想其他人代表您提交请求,请确保保护这个区域。

使用方法

安装和配置后,您可以从控制器中直接引用此服务。

<?php

use Pyrrah\OpenWeatherMapBundle\Services\Client;

/** @var Client $client */
$client = $this->get('pyrrah.openweathermap.client');

// Retrieve the current weather for Paris, FR
$weather = $client->getWeather('Paris,fr');

// Or retrieve the weather using the generic query method
$response = $client->query('weather', array('q' => 'Paris,fr'));
$weather = json_decode($response->getContent());

致谢

许可证

此包受 MIT 许可证保护。有关完整的版权和许可证信息,请参阅与源代码一起分发的《许可证文件》。