flerex/spain-gas
Composer 安装命令:
composer require flerex/spain-gas
包简介
A PHP client for the Geoportal Gasolineras API
README 文档
README
Spain Gas is a PHP client that act as a wrapper around Spain's Geoportal Gasolineras API. It has the following main features:
- Obtain data about all gas stations in Spain: addresses, geolocation, provided services, schedules and much more!
- Exact prices of fuel for every gas station in the country, updated multiple times every day.
- Beautifully designed interface using design patterns like the builder pattern.
- Statically typed with the latest PHP typing features.
- Properly tested with coverage higher than 80%.
Requirements
- You need
composerto be installed, either globally or locally in your project. - Compatible with PHP 7.4 or higher.
- PHP's extensions needed:
ext-jsonandext-simplexml.
Installation
To install this package be sure to have composer installed and run the following command in your project's directory:
composer require flerex/spain-gas
Usage
All the API's endpoints are available by using the GasApi class. Currently, there are only two endpoints for the API:
gasStations: This is the most useful endpoint. With it, you can find gas stations by different filters such as province or availability.towns: Returns a list ofTownobjects. Useful to obtain the ID of a specific town to use as filter in other endpoints.locateGasStations: This endpoint provides the geographical location of gas stations. The only difference with the previous endpoint is that provides a ranking of the gas station according to the price of the fuel (if specified as a filter).
NOTE: All the aforementioned endpoints will provide the price of the fuel if specified by using the query filter fuel. Otherwise, it will always return null.
Every endpoint returns an instance of a Builder class, which provides methods to filter the request to be made. Once you selected your filtering parameters, a call to method get is required to actually run the request and retrieve the data.
In case you need more information about what filters are available check the Documentation.
Endpoints
Here is a list of all available endpoints, and the differences in the data retrieved.
| Endpoint | Documentation | Ranking | Prices | Geographical Location | Station Information | Town ID |
|---|---|---|---|---|---|---|
| gasStations | Wiki | ✖️ | ✔️ * | ✔️ | ✔️ | ✖️ |
| locateGasStations | Wiki | ✔️ * | ✔️ * | ✔️ | ✖️ | ✖️ |
| towns | Wiki | ✖️ | ✖️ | ✖️ | ✖️ | ✔️ |
* Only returned when filtering by fuel.
Example
Obtain Stations by Fuel
Here is an example where we are retrieving all CNG gas stations in Spain:
use Flerex\SpainGas\Enums\Fuel; use Flerex\SpainGas\GasApi; $stations = GasApi::gasStations() ->fuel(Fuel::CNG()) ->get();
Get specific town ID
Let us suppose we needed to obtain all gas stations in A Coruña (city). To be able to filter by town in the gasStations endpoint, we will first need to retrieve the ID of the town.
use Flerex\SpainGas\Dtos\Town; use Flerex\SpainGas\Enums\Province; use Flerex\SpainGas\GasApi; $towns = GasApi::towns() ->province(Province::A_CORUNHA()) ->get(); $townId = array_filter($towns, fn(Town $town) => stripos($town->name, 'Coruña') !== false)[0]; // Everything up to this point could be done once and then be replaced with the obtained ID to avoid unnecessary API calls. $stations = GasApi::gasStations() ->town($townId) ->get();
Get the price ranking
Price rankings are used to categorize the price of some fuel in a gas station comparing it to their competitors. The algorithm used by the API is unknown.
Unfortunately, rankings are only available in the locateGasStations endpoints.
In the following example we are going to retrieve the cheapest self-service gas stations.
use Flerex\SpainGas\Dtos\GasStationLocation; use Flerex\SpainGas\Enums\Rank; use Flerex\SpainGas\Enums\Fuel; use Flerex\SpainGas\Enums\ServiceType; use Flerex\SpainGas\GasApi; $stations = GasApi::locateGasStations() ->serviceType(ServiceType::SELF_SERVICE()) ->fuel(Fuel::DIESEL_A()) ->get(); $cheapest = array_filter($stations, fn(GasStationLocation $station) => $station->rank->equals(Rank::CHEAP()));
NOTE: We need to specify a fuel so that rankings are provided in return.
Changelog
Changelog can be found in releases.
Copyright and License
Spain Gas was written by Flerex and is released under the MIT License.
© Flerex 2020
flerex/spain-gas 适用场景与选型建议
flerex/spain-gas 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63 次下载、GitHub Stars 达 6, 最近一次更新时间为 2021 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「client」 「geoportal」 「gasolineras」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 flerex/spain-gas 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 flerex/spain-gas 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 flerex/spain-gas 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
Last.fm API client for Laravel 7-13
统计信息
- 总下载量: 63
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-24
