smartsendio / api
Smart Send API 的 API 包
v0.2.0
2020-06-15 21:56 UTC
Requires
- php: ~7.2
- ext-json: *
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- mockery/mockery: ^1.3
- phpunit/phpunit: >=8.4
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-09-16 07:49:56 UTC
README
PHP SDK,用于与 Smart Send API 交互。
查看我们的Swagger 文档获取更多详细信息。
安装
通过 Composer
$ composer require smartsendio/api
用法
使用 Composer 的自动加载
require __DIR__.'/../vendor/autoload.php';
然后创建 SDK 的一个实例
$client = new Smartsendio\Api\Adapters\GuzzleClientAdapter(new \GuzzleHttp\Client()); $api = new Smartsendio\Api\ApiFactory($client); // Any client that implements ClientInterface can be used $api->apiToken('API_TOKEN_HERE')->website('WEBSITE'); // Set the authentication parameters
演示模式
演示模式可用于测试,并如下激活
$api->demo(); // Api is now in demo mode.
获取代理
使用 agents
API 获取代理。
// Example: All agents for a given carrier in a zipcode $response = $api->agents() // AgentApiInterface ->carrier('postnord') ->country('DK') ->zipcode('2100') ->get(); // PaginatedAgentApiResponseInterface // Example: The closest agents for a given carrier based on a given address $response = $api->agents() // AgentApiInterface ->carrier('postnord') ->country('DK') ->zipcode('2100') ->street('Nordre Frihavnsgade 1') ->closest(); // PaginatedAgentApiResponseInterface // Example: Get a single agent using the carries own unique agent number $response = $api->agents() // AgentApiInterface ->carrier('postnord') ->country('DK') ->lookup('1234567'); // AgentApiResponseInterface
运输
运输预订(创建运输标签)首先创建复杂的 Shipment
对象,然后将其传递给 shipments
API
$item = \Smartsendio\Api\Data\Item::make([ 'internal_id' => '000000123', 'internal_reference' => 'PRODUCT-1231456', 'sku' => '012345678', 'name' => 'Product A', 'description' => 'Small product A', 'hs_code' => '10203040', 'country_of_origin' => 'dk', 'image_url' => 'https://example.com/catalog/product-a.jpg', 'unit_weight' => 1.2, 'unit_price_excluding_tax' => 40.6, 'unit_price_including_tax' => 50.75, 'quantity' => 2, 'total_price_excluding_tax' => 81.2, 'total_price_including_tax' => 101.5, 'total_tax_amount' => 20.3, ]); $parcel = \Smartsendio\Api\Data\Parcel::make([ 'internal_id' => '00100025556', 'internal_reference' => 'ABC12345678', 'weight' => 9.3, 'height' => 1, 'width' => 1, 'length' => 1, 'freetext' => 'Please handle this package', 'total_price_excluding_tax' => 141.2, 'total_price_including_tax' => 176.5, 'total_tax_amount' => 35.3, ]); $parcel->addItem($item); $shipment = new \Smartsendio\Api\Data\Shipment(); $shipment->setReceiver([ 'internal_id' => '00000158895', 'internal_reference' => '123456', 'company' => 'Smart Send', 'name_line1' => 'Sven', 'name_line2' => 'Andersson', 'address_line1' => 'Drottninggatan 75', 'address_line2' => 'LGH 1102', 'postal_code' => '46133', 'city' => 'Trollhøttan', 'state' => 'AL', 'country' => 'SE', 'sms' => '+46851972000', 'email' => 'email@example.com', ])->addParcel($parcel); $response = $api->booking() // BookingApiInterface ->shipment($shipment); // BookingApiResponseInterface
处理错误
这是处理 API 错误的方法
$response = $api->booking()->shipment($shipment); // ApiResponseInterface $response->isSuccessful(); // false $error = $response->getError(); // ApiErrorInterface $error->getId(); // Unique id of the error $error->getCode(); // Error code describing the type of error $error->getMessage(); // Description of the error $error->getErrors(); // Return each individual error
变更日志
请参阅CHANGELOG以获取有关最近更改的更多信息。
测试
$ composer test
许可协议
MIT 许可协议 (MIT)。请参阅许可文件以获取更多信息。