chrissantiago82 / dhlparcel-php-api
PHP 的 DHL Parcel API 客户端
1.0.0
2024-09-13 09:12 UTC
Requires
- php: ^7.2 || ^8.0
- ext-json: *
- composer/ca-bundle: ^1.2
- guzzlehttp/guzzle: ^6.4 || ^7.1
- lcobucci/jwt: ^3.4 || ^4.0
- ramsey/uuid: ^4.0
- tightenco/collect: ^5.8 || ^6.0 || ^7.0 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^8.0 || ^9.4
- symfony/var-dumper: ^5.0
- vlucas/phpdotenv: ^5.0
This package is auto-updated.
Last update: 2024-09-13 09:21:36 UTC
README
安装
您可以通过 composer 安装此包
composer require mvdnbrk/dhlparcel-php-api
用法
初始化 DHL Parcel 客户端并设置您的凭证。
$dhlparcel = new \Mvdnbrk\DhlParcel\Client(); $dhlparcel->setUserId('your-user-id'); $dhlparcel->setApiKey('your-api-key');
如果您有多个账户,您可以设置一个账户 ID
$dhlparcel->setAccountId('123456');
创建包裹
$parcel = new \Mvdnbrk\DhlParcel\Resources\Parcel([ 'reference' => 'your own reference for the parcel (optional)', 'recipient' => [ 'first_name' => 'John', 'last_name' => 'Doe', 'street' => 'Poststraat', 'number' => '1', 'number_suffix' => 'A', 'postal_code' => '1234AA', 'city' => 'Amsterdam', 'cc' => 'NL', ], 'sender' => [ 'company_name' => 'Your Company Name', 'street' => 'Pakketstraat', 'additional_address_line' => 'Industrie 9999', 'number' => '99', 'postal_code' => '9999AA', 'city' => 'Amsterdam', 'cc' => 'NL', ], // Optional. This will be set as the default. 'pieces' => [ [ 'parcel_type' => \Mvdnbrk\DhlParcel\Resources\Piece::PARCEL_TYPE_SMALL, 'quantity' => 1, ], ], ]);
创建运输
$shipment = $dhlparcel->shipments->create($parcel); $shipment->id; // For shipments with multiple pieces: $shipment->pieces->each(function ($item) { $item->label_id; $item->barcode; }) // For a shipment with one single piece: $shipment->label_id; $shipment->barcode;
检索标签
可以通过使用 label_id 来检索标签。这将返回一个 PDF 标签字符串。
$dhlparcel->labels->get($shipment->label_id);
或者您可以直接将 Shipment 实例传递给此方法
$dhlparcel->labels->get($shipment);
传递
Shipment实例将只检索单个物品的运输标签,或者如果您创建了包含多个物品的运输,则是第一个物品的标签。如果您创建了包含多个物品的运输,您应该为运输中包含的每个物品单独检索标签。
设置包裹的投递选项
您可以在创建包裹时直接传递选项来设置包裹的投递选项
$parcel = new \Mvdnbrk\MyParcel\Resources\Parcel([ ... 'recipient' => [ ... ], 'options' => [ 'description' => 'Order #123', 'signature' => true, 'only_recipient' => true, 'cash_on_delivery' => 9.95, 'evening_delivery' => true, 'extra_assurance' => true, ... ], ]);
或者您可以在构建包裹后使用如 signature() 等方法
$parcel->onlyRecipient() ->signature() ->labelDescription('Order #123') ->cashOnDelivery(9.95) ->eveningDelivery() ->extraAssurance();
邮箱包裹
如果您想发送一个适合标准邮箱的包裹,可以使用 mailboxpackage() 方法
$parcel->mailboxpackage();
将包裹投递到 DHL 服务点
您可以将包裹发送到客户可以取件的 DHL 服务点。服务点的 ID 可以在创建包裹时直接设置,或者使用 servicePoint 方法
$parcel = new \Mvdnbrk\MyParcel\Resources\Parcel([ ... 'options' => [ 'service_point_id' => '8004-NL-272403', ... ], ]); $parcel->servicePoint('8004-NL-272403');
跟踪运输
$tracktrace = $dhlparcel->tracktrace->get('JVGL...'); // Check if the shipment is delivered: $tracktrace->isDelivered;
检索服务点
$servicepoints = $dhlparcel->servicePoints->setPostalcode('1012AA')->setHousenumber('1')->get();
这将返回一组 ServicePoint 对象
$servicepoints->each(function ($item) {
$item->id;
$item->name;
$item->latitude;
$item->longitude;
$item->distance;
$item->distanceForHumans();
});
与 Laravel 的使用
您可以通过使用 此包 将此包集成到您的 Laravel 应用程序中。
测试
composer test
更新日志
请参阅 更新日志 了解最近更改的详细信息。
贡献
请参阅 贡献指南 了解详细信息。
安全漏洞
请参阅我们关于如何报告安全漏洞的 安全策略。
致谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。