-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Dan Rogers edited this page Nov 5, 2022
·
3 revisions
The following should exemplify how to use the client library. These examples can also be found within the library's code.
#!/usr/bin/php
<?php
require __DIR__ . '/vendor/autoload.php';
use Shiptheory\Http\ShiptheoryClient;
use Shiptheory\Objects\AddressTaxNumberTypes;
use Shiptheory\Objects\ListShipmentQuery;
use Shiptheory\Objects\PackageQuery;
use Shiptheory\Objects\Product;
use Shiptheory\Objects\ProductQuery;
use Shiptheory\Objects\ProductSortParameters;
use Shiptheory\Objects\Recipient;
use Shiptheory\Objects\ReturnLabel;
use Shiptheory\Objects\SearchShipmentQuery;
use Shiptheory\Objects\Sender;
use Shiptheory\Objects\Shipment;
use Shiptheory\Objects\ShipmentDetail;
use Shiptheory\Objects\ShipmentProduct;
use Shiptheory\Objects\TaxNumber;
$client = new ShiptheoryClient('test@test.com', 'Password123!');
// Set Shipment Details
$shipment_detail = (new ShipmentDetail())
->setWeight(1)
->setParcels(1)
->setValue(1)
->setShippingPrice(2.99)
->setReference3('ORDERREF3')
->setSalesSource('My Store')
->setShipDate('2023-04-30')
->setCurrencyCode('GBP');
// Set Recipient
$reciever = (new Recipient())
->setCompany('Shiptheory')
->setFirstname('Test')
->setLastname('Customer')
->setAddressLine1('Unit 4.1 Paintworks')
->setAddressLine2('Bath Road')
->setCity('Bristol')
->setCountry('GB')
->setPostcode('BS4 3EH')
->setEmail('recipient@test.com')
->setTelephone('01234567890')
->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())
->setCompany('Shiptheory')
->setFirstname('Test')
->setLastname('Shipper')
->setAddressLine1('Bristol Old Vic')
->setAddressLine2('King Street')
->setCity('Bristol')
->setCountry('GB')
->setPostcode('BS1 4ED')
->setEmail('sender@test.com')
->setTelephone('01234567890');
// Set Product
$product = (new ShipmentProduct())
->setName('My Test Product')
->setSku('TestProd1')
->setQty(1)
->setValue(1)
->setWeight(1)
->setCommodityCode('8443991000')
->setCommodityManucountry('PL');
//Start new shipment data object
$shipment = (new Shipment())
->setReference('4566466654')
->setReference2('Test5678')
->setShipmentDetail($shipment_detail)
->setRecipient($reciever)
->setSender($sender)
->setProducts([$product]);
// Send shipment to Shiptheory
$data = $shipment->toJson(true);
$result = $client->bookShipment($data);
var_dump($result);
// View a shipment
var_dump($client->viewShipment('Test1234'));
// List shipments
$list_fields = [
'channel_name' => 'Api',
'status' => 'Ignored',
'limit' => 1
];
$list_query = new ListShipmentQuery($list_fields);
var_dump($client->listShipment($list_query->toQueryParams()));
// Search for shipments
$search_fields = [
'created_from' => '2022-04-01',
'created_to' => '2022-04-30',
'include_products' => 1,
];
$search_query = new SearchShipmentQuery($search_fields);
var_dump($client->searchShipment($search_query->toQueryParams()));
// Search for packages
$package_fields = [
'length' => 25,
'width' => 25,
'height' => 25,
];
$package_query = new PackageQuery($package_fields);
var_dump($client->getPackageSizes($package_query->toQueryParams()));
// List services
var_dump($client->getOutgoingDeliveryServices());
// Create a return label
$label = (new ReturnLabel())
->setOutgoingReference('Test1234')
->setDeliveryPostcode('BS4 3EH')
->setReturnService(1)
->setExpiry('2022-05-30');
$data = $label->toJson(true);
var_dump($client->createReturnLabel($data));
// Add a new Product
$product = (new Product())
->setSku('API-Product-1')
->setName('API Test Product')
->setPrice(1.99)
->setWeight(1.5)
->setBarcode('0123456789')
->setCommodityCode('8443991000')
->setCommodityManucountry('GB')
->setCommodityDescription('This is a test product')
->setCommodityComposition('Electronic components')
->setLength(25)
->setWidth(27.69)
->setHeight(4.66);
$data = $product->toJson(true);
var_dump($client->addProduct($data));
// View a product
var_dump($client->viewProduct('API-Product-1'));
// Update a product
$product = (new Product())->setPrice(5.99);
$data = $product->toJson(true);
var_dump($client->updateProduct('API-Product-1', $data));
// List products
$product_fields = [
'sort' => ProductSortParameters::PRICE,
'limit' => 25
];
$product_query = new ProductQuery($product_fields);
var_dump($client->listProducts($product_query->toQueryParams()));