weble/ptv-php
Composer 安装命令:
composer require weble/ptv-php
包简介
PTV Api PHP SDK
README 文档
README
This is an SDK for the PTV API.
It currently implements partially the Data API and the Routing API.
Under the hood it uses Saloon to handle the requests.
It features only 2 dependencies:
saloonphp/saloonto handle the requests and the SDK building in generalmoneyphp/moneyto represent the prices and currencies
Every parameter and response object is carefully mapped with a dedicated DTO class and Enum
Installation
composer require weble/ptv-php
Basic Usage
Just create the client and interact with each of the apis.
use PTV\PTV; $ptv = new PTV('[YOUR-API-KEY]'); $profiles = $ptv->data()->vehicleProfiles()->all(); $route $ptv->routing()->route()->calculate(['lat,lng', 'lat2,lng2']);
Setting a language
use PTV\PTV; $ptv = new PTV('[YOUR-API-KEY]', 'it');
Data Api
Only 3 endpoints are implemented as of today:
1. vehicleProfiles
use PTV\PTV; $ptv = new PTV('[YOUR-API-KEY]'); $profiles = $ptv->data()->vehicleProfiles()->all();
2. vehicleModels
use PTV\Data\Enums\VehicleType; use PTV\PTV; $ptv = new PTV('[YOUR-API-KEY]'); $profiles = $ptv->data()->vehicleModels()->all(); $profiles = $ptv->data()->vehicleModels()->all([ VehicleType::TRAILER, VehicleType::SEMI_TRAILER, ]);
3. mapInformation
use PTV\Data\Enums\VehicleType; use PTV\PTV; $ptv = new PTV('[YOUR-API-KEY]'); $mapInfo = $ptv->data()->mapInformation()->all();
Routing Api
Currently only 3 endpoints are supported:
1. calculate
This is by far the most complete one and the most likely used. You can calculate a route by chaining parameters within the call.
Each parameter is typed for full IDE autocompletion and ease of use.
use Money\Currency; use PTV\Data\Enums\EngineType; use PTV\Data\Enums\FuelType; use PTV\PTV; use PTV\Routing\DTO\MonetaryCostOptions; use PTV\Routing\DTO\Options; use PTV\Routing\DTO\Vehicle; use PTV\Routing\Enums\ResultType; use PTV\Routing\Enums\TrafficMode; $ptv = new PTV('[YOUR-API-KEY]'); $route = $ptv ->routing() ->route() ->return([ ResultType::MONETARY_COSTS, ResultType::POLYLINE, ResultType::LEGS, ResultType::LEGS_POLYLINE, ResultType::ROUTE_ID, ResultType::TOLL_COSTS, ResultType::TOLL_SYSTEMS, ResultType::TOLL_SECTIONS, ResultType::TOLL_EVENTS, ResultType::ALTERNATIVE_ROUTES, ResultType::GUIDED_NAVIGATION, ]) ->forVehicle( new Vehicle( engineType: EngineType::COMBUSTION, fuelType: FuelType::DIESEL, numberOfAxles: 2, totalPermittedWeight: 75000, ) ) ->withCostOptions( new MonetaryCostOptions( costPerKilometer: 1.2 ) ) ->withOptions( new Options( startTime: DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2025-01-01 09:00:00'), trafficMode: TrafficMode::AVERAGE, currency: new Currency('EUR'), ) ) ->calculate([ "45.5422993,11.5220921", "53.5418064,9.9991367" ])
2. recalculate
Uses a previously returned Route Id to recalculate parts of the route results
use Money\Currency; use PTV\Data\Enums\EngineType; use PTV\Data\Enums\FuelType; use PTV\PTV; use PTV\Routing\DTO\MonetaryCostOptions; use PTV\Routing\DTO\Options; use PTV\Routing\DTO\Vehicle; use PTV\Routing\Enums\ResultType; use PTV\Routing\Enums\TrafficMode; $ptv = new PTV('[YOUR-API-KEY]'); $route = $ptv ->routing() ->route() ->return([ ResultType::MONETARY_COSTS, ]) ->withCostOptions( new MonetaryCostOptions( costPerKilometer: 1.2 ) ) ->withOptions( new Options( startTime: DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2025-01-01 09:00:00'), trafficMode: TrafficMode::AVERAGE, currency: new Currency('EUR'), ) ) ->recalculate('[your-route-id]');
Get Route
Get previously calculated route details, or even an alternative route detail.
use Money\Currency; use PTV\Data\Enums\EngineType; use PTV\Data\Enums\FuelType; use PTV\PTV; use PTV\Routing\DTO\MonetaryCostOptions; use PTV\Routing\DTO\Options; use PTV\Routing\DTO\Vehicle; use PTV\Routing\Enums\ResultType; use PTV\Routing\Enums\TrafficMode; $ptv = new PTV('[YOUR-API-KEY]'); $route = $ptv ->routing() ->route() ->get('[your-route-id]');
The route object
The Route object is a fully typed DTO to ease reading the results of the APIs
use PTV\Routing\DTO\Route; /** @var Route $route **/ $route->alternativeRoutes; $route->monetaryCosts->distanceCost; $route->toll->costs; // ...
Testing
To test you can just run
composer test
It will use fixture json to test the SDK.
If you want you can also set a .env file with a dedicated PTV key and do some real testing with the API.
weble/ptv-php 适用场景与选型建议
weble/ptv-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 882 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 weble/ptv-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 weble/ptv-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 882
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-23