lfischer/open-weather-map-api
Composer 安装命令:
composer require lfischer/open-weather-map-api
包简介
A simple PHP library for querying the Open Weather Map API.
README 文档
README
This library will help you integrate the Open Weather Map API to your webservice. It is lightweight and provides all necessary code including a lot of convenience methods.
Quick start
Using the API is very easy - you'll only need to provide a API key (Get one here) in order to use it. There are endpoints for retrieving different weather data by predefined conditions, like "by country and city", "by latitude and longitude", "by airport code" or even "by IP address".
Simply create a API instance and get the endpoint you need. These endpoints will contain convenience methods with
typed parameters to receive the desired data.
use lfischer\openWeatherMap\API; // Get weather data by a city and (optional) country name. $weather = (new API('<API-key here>')) ->getCurrentWeather() ->byCityName('Dusseldorf,Germany'); // Get weather data by a "city ID". $weather = (new API('<API-key here>')) ->getCurrentWeather() ->byCityId(524901);
Default options
Most endpoints can define a language to get API responses in your language. For more details scroll down :)
By default you will receive an array with the data you like. Some endpoints can be used with different modes like "XML" and "HTML".
Also it is possible to use different units like "metric" and "imperial".
API endpoints for data collection
The goal is to implement PHP classes for every data collection API endpoint. The main
API class has accessor methods for each available endpoint.
$api->getClimateForecast()returns an instance ofClimateForecastEndpoint.$api->getCurrentWeather()returns an instance ofCurrentWeatherEndpoint.$api->getHourlyForecast()returns an instance ofHourlyForecastEndpoint.$api->getDailyForecast()returns an instance ofDailyForecastEndpoint.$api->getOneCall()returns an instance ofOneCallEndpoint.
Sadly some endpoints require a paid subscription which I don't have. If anyone likes to contribute or test the affected endpoints please let me know!
More convenience
The API client provides convenience methods and classes to set different modes (xml, json or html),
units (metric or imperial) or languages (49 languages available).
Use the constants located in Mode, Unit and Language to get the correct values and autocompletion in your IDE.
// Get weather data by a city and (optional) country name. use lfischer\openWeatherMap\API; use lfischer\openWeatherMap\Parameter\Language; use lfischer\openWeatherMap\Parameter\Mode; use lfischer\openWeatherMap\Parameter\Unit; $weather = (new API('<API-key here>')) ->getCurrentWeather() ->setMode(Mode::XML) ->setUnit(Unit::METRIC) ->setLanguage(Language::ENGLISH) ->byCityName('Dusseldorf,Germany');
Different Request options
The API client can use three different request adapters, according to your environment and possible other libraries.
cURL
In case you can not use file_get_contents, you can use the 'Curl' request adapter. For this your environment needs
to provide the curl PHP extension.
Dump
The Dump request adapter can be used to return the prepared URL in case you have your own request library to work
with. This is also used for testing.
Guzzle
If you are a fan of the great Guzzle library, you can use this adapter and make use of all its great features :)
Simple
The 'Simple' request adapter makes use of file_get_contents (just as the previous versions). For this adapter you will
need to activate the fopen wrappers.
Response types
According to the mode option the response will be returned in a different Response class. Every response type has a
getResponse method that returns the specific set of data.
HtmlResponse->getResponse()returns astringJsonResponse->getResponse()returns aarrayXmlResponse->getResponse()returns aSimpleXMLElement
Each type also contains a getRawResponse method which will return the raw string response from the API, in case
you'd like to process the response on your own.
Contribution
As mentioned before I do not have a paid subscription in order to develop and test every data collection. For this I need YOUR help ;)
If anyone likes to contribute or test the affected endpoints please let me know!
lfischer/open-weather-map-api 适用场景与选型建议
lfischer/open-weather-map-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 140 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 02 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「weather」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lfischer/open-weather-map-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lfischer/open-weather-map-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lfischer/open-weather-map-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel-forecast provides a service provider and a facade around the Forecast-php wrapper
A PSR-7 compatible library for making CRUD API endpoints
Add a weather widget to Flarum
Simple weather forecaster for Laravel
Unofficial PHP client for aWhere weather API
Anax weather module
统计信息
- 总下载量: 140
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-01