tomoehlrich / taxifarefinder
为TaxiFareFinder API提供的PHP/Laravel封装
1.0.1
2020-03-07 14:02 UTC
Requires
- php: ^7.2
- guzzlehttp/guzzle: ^6.3
- illuminate/support: ^5.0|^6.0|^7.0
Requires (Dev)
- phpunit/phpunit: ^7.5|^8.0|^8.5
This package is auto-updated.
Last update: 2024-09-08 00:05:33 UTC
README
本包可以从TaxiFareFinder API获取支持的城市、出租车公司和出租车费用信息。
您可以在https://www.taxifarefinder.com/contactus.php下申请TaxiFareFinder API密钥。
安装
本包可以通过composer安装。
composer require tomoehlrich/taxifarefinder
Laravel安装
本包可用于Laravel和非Laravel项目。
在Laravel 5.5+中,本包将使用Laravel的自动注册功能。在Laravel的旧版本中,您必须手动安装包的服务提供者和外观。
// config/app.php 'providers' => [ // ... TomOehlrich\TaxiFareFinder\TaxiFareFinderServiceProvider::class ];
// config/app.php 'aliases' => [ // ... 'TaxiFareFinder' => TomOehlrich\TaxiFareFinder\Facades\TaxiFareFinder::class, ]
接下来,您需要发布配置文件
php artisan vendor:publish --provider="TomOehlrich\TaxiFareFinder\TaxiFareFinderServiceProvider"
配置文件只包含一个选项。请确保您的API密钥在此可用。
return [ /* |-------------------------------------------------------------------------- | TaxiFareFinder API Key |-------------------------------------------------------------------------- | | The TaxiFareFinder API Key can be requested at | https://www.taxifarefinder.com/contactus.php | */ 'api_key' => env('TAXIFAREFINDER_API_KEY', ''), ];
使用方法
首先创建一个TaxiFareFinder类的新实例。
$tff = new TaxiFareFinder('<YOUR API KEY>');
您可以获取API支持的最接近的城市
return $tff->getNearestCity(1.290270, 103.851959); /* This function returns the following array: Array ( [name] => Singapore [full_name] => Singapore, Singapore [handle] => Singapore [locale] => zh_SG [distance] => 7747 )
您可以获取出租车费用的详细信息。
return $tff->getTaxiFare(42.368025,-71.022155, 42.362571,-71.055543); /* This function returns the following array: Array ( [total_fare] => 20.61 [initial_fare] => 2.6 [metered_fare] => 10.42 [tip_amount] => 2.69 [tip_percentage] => 15 [locale] => en_US [currency] => Array ( [int_symbol] => USD ) [rate_area] => Boston, MA [flat_rates] => Array ( ) [extra_charges] => Array ( [0] => Array ( [charge] => 2.25 [description] => Airport Fee ) [1] => Array ( [charge] => 2.65 [description] => Harbor tunnel toll ) ) [distance] => 4591.1 [duration] => 544 )
您可以获取一个城市中的出租车公司列表。
return $tff->getTaxiCompanies('Ho-Chi-Minh-Vietnam'); /* This function returns the following array: Array ( [businesses] => Array ( [0] => Array ( [name] => DichungTaxi [phone] => 093-607-0416 [type] => taxi ) ) )
在Laravel中,您可以直接调用
return TaxiFareFinder::getNearestCity(1.290270, 103.851959);
要捕获异常,您应该在方法周围使用try .. catch块。
try { $fare = $tff->getTaxiFare(42.368025,-71.022155, 42.362571,-71.055543); } catch(TomOehlrich\TaxiFareFinder\Exceptions\TffException $e) { return $e->getMessage(); } catch(\Exception $e) { return $e->getMessage(); }
本包附带了一些测试。只需在tests/TestCase.php中输入您的API密钥,然后运行
composer test