foodkit / lalamove-php-api
Lalamove API 的 PHP 客户端
4.0.0
2023-02-15 09:32 UTC
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.4.2
- nesbot/carbon: ^2.57.0
- psr/log: ^1.1 || ^2.0 || ^3.0
Requires (Dev)
- mockery/mockery: ^1.5
- phpunit/phpunit: ^9.5
README
此库提供了 Lalamove API (v2 和 v3) 的 PHP 封装。目前支持 PHP >= 7.4|8.1
由 Foodkit 构建和维护。
运行测试
$ ./vendor/bin/phpunit tests/
PHPUnit 9.5.19 #StandWithUkraine
............................................... 47 / 47 (100%)
Time: 00:09.259, Memory: 10.00 MB
OK (47 tests, 142 assertions)
使用库
<?php $settings = new \Lalamove\Client\V3\Settings( 'https://sandbox-rest.lalamove.com', 'API_KEY', 'API_SECRET', \Lalamove\Client\V3\Settings::COUNTRY_SINGAPORE // country ); $client = new Lalamove\Client\V3\Client($settings); ////// // Create a quote: $quotation = new \Lalamove\Requests\V3\Quotation(/* parameters here */); // ...prepare the quotation object... $quotationResponse = $client->quotations()->create($quotation); // Get quotation by id $quotationDetailsResponse = $client->quotations()->get($quotation->quotationId); ////// // Create an order // Provide the quotationID and stopId received from create quote and add contact information for both the sender and recipients $contact = new \Lalamove\Requests\V3\Contact('Contact Name', '+65991111110', 'stop_id_from_quotation'); // recipient contact and instruction per stop $recipients = [ [ 'stopId' => 'stop_id_1', 'name' => 'name', // Must be a valid number with region code (ex: +65) 'phone' => '+65991111111', ], [ 'stopId' => 'stop_id_2', 'name' => 'name', // Must be a valid number with region code (ex: +65) 'phone' => '+65991111112', ] ]; $order = new \Lalamove\Requests\V3\Order($quotationId, $sender, $recipients); $orderResponse = $client->orders()->create($order); // Fetch order details: $details = $client->orders()->details($orderResponse->orderId); // Get the driver: // driverId from create order or by order details response $driver = $client->drivers()->get($details->orderId, $details->driverId); // Cancel the order: $details = $client->orders()->cancel($orderResponse->orderId); ////// // Create a webhook $webhook = new \Lalamove\Requests\V3\Webhook('https://webhook.site/fd8ccc58-7447-4122-8a0c-f9c31eb79ad3'); $webhook = $client->webhooks()->create($webhook));
错误
客户端库在请求(或服务器)错误的情况下会抛出异常。
try { $client->orders()->create($order); } catch (\Lalamove\Exceptions\PaymentRequiredException $ex) { echo 'Error: not enough funds in Lalamove wallet to create the order'; }
您可以独立处理这些异常(请参阅 src/Exceptions/
中的异常),或者只需捕获超类型 Lalamove\Exceptions\LalamoveException
来处理所有失败情况。
try { $client->orders()->create($order); } catch (\Lalamove\Exceptions\LalamoveException $ex) { echo "Error: I don't know what happened, but the request failed for some reason."; }
贡献
在 master 分支上创建 PR。请使用 PSR-x 规范,并包含测试。
设计目标
- 依赖 IDE。我们应该利用 IDE(自动完成)来帮助开发者使用库。
- 尽可能隐藏传输机制(HTTP)。除非绝对必要(例如端点配置、请求超时),否则最终用户在使用库时不应担心 HTTP 概念。
- 接口应易于使用/不引人注目。库应尽可能减少开发过程中的摩擦。
许可证
请参阅 LICENSE。