dan-rogers/shiptheory-php

用于与Shiptheory API通信的API客户端

v2.2.1 2023-01-26 16:16 UTC

This package is auto-updated.

Last update: 2024-09-07 16:49:11 UTC


README

存在Python版本: Python的Shiptheory API库

  • 要创建pull请求,请分支此存储库(请遵守pull请求模板中的指南)。
  • 使用GitHub问题详细说明遇到的问题。

功能

  • 轻松构建Shiptheory请求体和查询。
  • 针对每个Shiptheory端点的专用方法。
  • 自动令牌认证。只需提供您的凭证,库将根据您的需要保持令牌有效。
  • 标准化的错误消息。
  • 通过.env配置

安装

使用composer要求

composer require dan-rogers/shiptheory-php

配置

该库目前使用.env文件配置库的某些方面。

重要:它不使用任何版本的phpdotenv,而是使用自己的自定义实现以保持其轻量级。

.env.example中的值复制到项目根目录下新或现有的.env文件中。

当前支持的配置选项

  • SHIPTHEORY_PHP_LOG_REQUESTS - (布尔值) 当设置为true时,请求将被记录。当false或不存在时,请求不会记录。
  • SHIPTHEORY_PHP_SSL_VERIFY_PEER - (布尔值) 当设置为true时,启用SSL验证。当false或不存在时,请求不使用SSL验证。
  • SHIPTHEORY_PHP_LOG_PATH - (字符串) 当设置字符串时,它用作生成日志文件的文件夹的路径。

使用Shiptheory合作伙伴标签

"如果你正在开发一个将被超过1家公司使用或你打算以任何方式分发的应用程序,你必须在你所有的请求中包含Shiptheory-Partner-Tag http请求头。请联系Shiptheory支持以获取合作伙伴标签。这无需付费,标签用于为顾客和合作伙伴提供更好的支持。"- API文档

为了将合作伙伴标签添加到API请求中,在创建新的ShiptheoryClient时将其作为第三个参数添加。

$client = new ShiptheoryClient('test@test.com', 'Password123!', 'my_partner_tag');

使用方法

对于所有端点、查询和预订,工作流程类似。示例存在于Examples文件中。如果你想要查看所有端点方法,请查看src/Http/ShiptheoryClient.php

建议您使用提供的对象来构建您的请求,因为这将每次都生成完美的请求体。然而,如果你愿意,你也可以只是将查询参数或JSON体(根据请求类型)作为字符串提供给您调用的方法。

查看货物的示例

$client = new ShiptheoryClient('test@test.com', 'Password123!');
$result = $client->viewShipment('Test1234')

列出货物的示例

$client = new ShiptheoryClient('test@test.com', 'Password123!');
$list_fields = [
    'channel_name' => 'Api',
    'status' => 'Ignored',
    'limit' => 1
];
$list_query = new ListShipmentQuery($list_fields);
$params = $list_query->toQueryParams();
$client->listShipment($params);

搜索货物的示例

$client = new ShiptheoryClient('test@test.com', 'Password123!');
$search_fields = [
    'created_from' => '2022-04-01',
    'created_to' => '2022-04-30',
    'include_products' => 1,
];
$search_query = new SearchShipmentQuery($search_fields);
$params = $search_query->toQueryParams();
$client->searchShipment($params);

预订货物的示例

$client = new ShiptheoryClient('test@test.com', 'Password123!');
//Start new shipment data object
$shipment = new Shipment();
$shipment->setReference('Test1234');
$shipment->setReference2('Test5678');

// Set Shipment Details
$shipment_detail = new ShipmentDetail();
$shipment_detail->setWeight(1);
$shipment_detail->setParcels(1);
$shipment_detail->setValue(1);
$shipment_detail->setShippingPrice(2.99);
$shipment_detail->setReference3('ORDERREF3');
$shipment_detail->setSalesSource('My Store');
$shipment_detail->setShipDate('2022-04-30');
$shipment_detail->setCurrencyCode('GBP');

// Set Recipient
$reciever = new Recipient();
$reciever->setCompany('Shiptheory');
$reciever->setFirstname('Test');
$reciever->setLastname('Customer');
$reciever->setAddressLine1('Unit 4.1 Paintworks');
$reciever->setAddressLine2('Bath Road');
$reciever->setCity('Bristol');
$reciever->setCountry('GB');
$reciever->setPostcode('BS4 3EH');
$reciever->setEmail('recipient@test.com');
$reciever->setTelephone('01234567890');
$reciever->setWhat3Words('///what.three.words');
$eori = new TaxNumber('GB205672212000', AddressTaxNumberTypes::EORI);
$vat = new TaxNumber('GB123456789', AddressTaxNumberTypes::VAT);
$reciever->setTaxNumbers([$eori, $vat]);

// Set sender
$sender = new Sender();
$sender->setCompany('Shiptheory');
$sender->setFirstname('Test');
$sender->setLastname('Shipper');
$sender->setAddressLine1('Bristol Old Vic');
$sender->setAddressLine2('King Street');
$sender->setCity('Bristol');
$sender->setCountry('GB');
$sender->setPostcode('BS1 4ED');
$sender->setEmail('sender@test.com');
$sender->setTelephone('01234567890');

// Set Product
$product = new Product();
$product->setName('My Test Product');
$product->setSku('TestProd1');
$product->setQty(1);
$product->setValue(1);
$product->setWeight(1);
$product->setCommodityCode('8443991000');
$product->setCommodityManucountry('PL');

// Add all elements to the shipment
$shipment->setShipmentDetail($shipment_detail);
$shipment->setRecipient($reciever);
$shipment->setSender($sender);
$shipment->setProducts([$product]);

// Send shipment to Shiptheory
$data = $shipment->toJson(true);
$result = $client->bookShipment($data);