by Michał Biarda
This package was created so you could easily connect your PHP app with Inpost ShipX API.
The documentation of ShipX API may be found here: https://docs.inpost24.com/display/PL/API+ShipX
The library is open sourced. Feel free to contribute!
The recommended way to install this package is through Composer.
composer require michalbiarda/shipx-php-sdk
Watchout: This package uses HTTPlug for HTTP client abstraction. Please check their official documentation to understand how to configure it properly with your project.
All calls to ShipX API are made through \MB\ShipXSDK\Client\Client
object.
To create it, you need to provide API URL (production or sandbox) and your API key (for some actions API key is not needed).
$client = new \MB\ShipXSDK\Client\Client(
'https://sandbox-api-shipx-pl.easypack24.net',
'y0urs3cr3tc0d3'
);
To call the API endpoint you need to use callMethod
method of the client.
It takes the following arguments:
$method
- API method that you want to run; all methods can be found in subfolders ofsrc/Method/
folder; you can add your own methods if you need$uriParams
- array of URI params needed for specific method; you can find required params in method'sgetUriTemplate
method; example: for URI template/v1/organizations/:organization_id/address_books
the required URI param isorganization_id
$queryParams
- array of query params that may be used for some methods to make your call more specific:- if a method implements
\MB\ShipXSDK\Method\WithPaginatedResultsInterface
you can use the following params:page
- to specify which page of multipaged result you'd like to get,
- if a method implements
\MB\ShipXSDK\Method\WithSortableResultsInterface
you can use the following params:sort_by
- to specify by which field should the results be sorted; possible values might be found in method'sgetSortableFields
method,sort_order
- to specify the direction of sorting; possible values areasc
anddesc
,
- if a method implements
\MB\ShipXSDK\Method\WithFilterableResultsInterface
you:- can use all the params defined in method's
getFilters
method,
- can use all the params defined in method's
- if a method implements
\MB\ShipXSDK\Method\WithQueryParamsInterface
you:- have to use all the params defined in method's
getRequiredQueryParams
method, - can use all the params defined in method's
getOptionalQueryParams
method
- have to use all the params defined in method's
- if a method implements
$payload
- data transfer object (\MB\ShipXSDK\DataTransferObject\DataTransferObject
) required for all methods that implement\MB\ShipXSDK\Method\WithJsonRequestInterface
; the name of required object might be found in method'sgetRequestPayloadModelName
name.
callMethod
method returns \MB\ShipXSDK\Response\Response
object.
You can check if the call was successful by running response's getSuccess
method.
The body of the response might be taken by running response's getPayload
method. It will be one of the following:
\MB\ShipXSDK\Model\Error
object if error occurred,\MB\ShipXSDK\Model\BinaryContent
object if method implements\MB\ShipXSDK\Method\WithBinaryResponseInterface
and correct binary file was returned by API,- data transfer object which name can be found in method's
getResponsePayloadModelName
method, if method implements\MB\ShipXSDK\Method\WithJsonResponseInterface
, null
if error haven't occurred and method doesn't expect any body.
callMethod
method might throw:
\MB\ShipXSDK\Exception\InvalidModelException
if the response body doesn't match expected data transfer object,\GuzzleHttp\Exception\GuzzleException
if something went wrong with the connection.
$addressForm = new \MB\ShipXSDK\Model\AddressForm();
$addressForm->city = 'Warszawa';
$addressForm->post_code = '01-234';
$addressForm->country_code = 'PL';
$addressForm->street = 'Testowa 11';
$addressForm->building_number = '12/23';
$receiver = new \MB\ShipXSDK\Model\TransactionPartyForm();
$receiver->phone = '123456789';
$receiver->email = '[email protected]';
$receiver->first_name = 'Michał';
$receiver->last_name = 'Testowy';
$receiver->address = $addressForm;
$dimensions = new \MB\ShipXSDK\Model\DimensionsSimple();
$dimensions->height = 21.5;
$dimensions->length = 2.1;
$dimensions->width = 1.7;
$weight = new \MB\ShipXSDK\Model\WeightSimple();
$weight->amount = 2.0;
$parcel = new \MB\ShipXSDK\Model\ParcelsSimple();
$parcel->dimensions = $dimensions;
$parcel->weight = $weight;
$parcel->is_non_standard = false;
$shipmentOfferForm = new \MB\ShipXSDK\Model\ShipmentOfferForm();
$shipmentOfferForm->receiver = $receiver;
$shipmentOfferForm->parcels = [$parcel];
$shipmentOfferForm->additional_services = [];
$response = $client->callMethod(
new \MB\ShipXSDK\Method\Shipment\CreateOffer(),
['organization_id' => '1234'],
[],
$shipmentOfferForm
);
if ($response->getSuccess()) {
/** @var \MB\ShipXSDK\Model\Shipment $payload */
$payload = $response->getPayload();
echo 'Shipment ID is ' . $payload->id;
}
Endpoints covered: 44/57
Endpoints with integration tests: 38/57
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\Create
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulSimpleFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\CreateOffer
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulOfferFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\Buy
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulOfferFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\SelectOffers
Integration test: No
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\SelectOffer
Integration test: No
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\BuyBulk
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulBatchFlowWithoutBuying
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\CreateBatch
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulBatchFlowWithBuying
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\GetBatch
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulBatchFlowWithBuying
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\Cancel
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulCancellation
Note: From time to time Sandbox API responds with empty body instead of error payload. Empty body is expected for successful call.
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\GetLabel
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulSimpleFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\GetLabels
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulBatchFlowWithBuying
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\GetReturnLabels
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulSimpleFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\Shipment\GetList
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testGetListSuccessCall
Note: Quite often Sandbox shipment search cannot find shipment by ID, even though it is reachable using standard get method.
Documentation: Link
Method: \MB\ShipXSDK\Method\Status\GetList
Integration test: \MB\ShipXSDK\Test\Integration\Client\StatusResourceTest::testGetListSuccessfulCall
Documentation: Link
Method: \MB\ShipXSDK\Method\Tracking\Read
Integration test: \MB\ShipXSDK\Test\Integration\Client\TrackingResourceTest::testReadSuccessfulCall
Note: Sandbox API constantly responds with "Resource not found" error.
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: \MB\ShipXSDK\Method\User\GetList
Integration test: No
Note: Sandbox API constantly responds with "Resource not found" error.
Documentation: Link
Method: \MB\ShipXSDK\Method\Organization\Read
Integration test: \MB\ShipXSDK\Test\Integration\Client\OrganizationResourceTest::testReadSuccessfulCall
Documentation: Link
Method: \MB\ShipXSDK\Method\Organization\GetList
Integration test: \MB\ShipXSDK\Test\Integration\Client\OrganizationResourceTest::testGetListSuccessfulCall
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchPoint\Read
Integration test: No
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchPoint\GetList
Integration test: No
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchOrder\Create
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulDispatchOrderFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchOrder\Read
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulDispatchOrderFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchOrder\Delete
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulDispatchOrderFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchOrder\GetList
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulDispatchOrderFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchOrder\CreateComment
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulDispatchOrderFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchOrder\UpdateComment
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulDispatchOrderFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchOrder\DeleteComment
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulDispatchOrderFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchOrder\Calculate
Integration test: No
Note: Each correct request to Sandbox API responds with the following error: "Action available only for prepaid users."
Documentation: Link
Method: \MB\ShipXSDK\Method\DispatchOrder\GetPrintout
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulDispatchOrderFlow
Method: \MB\ShipXSDK\Method\DispatchOrder\GetPrintouts
Integration test: \MB\ShipXSDK\Test\Integration\Client\ShipmentResourceTest::testSuccessfulDispatchOrderFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\AddressBook\GetList
Integration test: \MB\ShipXSDK\Test\Integration\Client\AddressBookResourceTest::testSuccessfulCrudFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\AddressBook\Read
Integration test: \MB\ShipXSDK\Test\Integration\Client\AddressBookResourceTest::testSuccessfulCrudFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\AddressBook\Create
Integration test: \MB\ShipXSDK\Test\Integration\Client\AddressBookResourceTest::testSuccessfulCrudFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\AddressBook\Update
Integration test: \MB\ShipXSDK\Test\Integration\Client\AddressBookResourceTest::testSuccessfulCrudFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\AddressBook\Delete
Integration test: \MB\ShipXSDK\Test\Integration\Client\AddressBookResourceTest::testSuccessfulCrudFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\Service\GetList
Integration test: \MB\ShipXSDK\Test\Integration\Client\ServiceResourceTest::testGetListSuccessfulCall
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: No
Integration test: No
Documentation: Link
Method: \MB\ShipXSDK\Method\SendingMethod\GetList
Integration test: \MB\ShipXSDK\Test\Integration\Client\SendingMethodResourceTest::testGetListSuccessfulCall
Documentation: Link
Method: \MB\ShipXSDK\Method\Mpk\GetList
Integration test: \MB\ShipXSDK\Test\Integration\Client\MpkResourceTest::testSuccessfulCruFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\Mpk\Create
Integration test: \MB\ShipXSDK\Test\Integration\Client\MpkResourceTest::testSuccessfulCruFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\Mpk\Update
Integration test: \MB\ShipXSDK\Test\Integration\Client\MpkResourceTest::testSuccessfulCruFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\Mpk\Read
Integration test: \MB\ShipXSDK\Test\Integration\Client\MpkResourceTest::testSuccessfulCruFlow
Documentation: Link
Method: \MB\ShipXSDK\Method\Report\GetCod
Integration test: \MB\ShipXSDK\Test\Integration\Client\ReportResourceTest::testGetCodSuccessfulCall
Documentation: Link
Method: \MB\ShipXSDK\Method\Point\GetList
Integration test: \MB\ShipXSDK\Test\Integration\Client\PointResourceTest::testGetListSuccessfulCall
Documentation: Link
Method: \MB\ShipXSDK\Method\Point\Read
Integration test: \MB\ShipXSDK\Test\Integration\Client\PointResourceTest::testReadSuccessfulCall
The main known issue is instability of Sandbox API. From time to time newly created shipments are processed very slowly or end up in a void. Because of that integration tests sometimes fail unexpectedly. I guess we need to live with it...