xi0s/royal-mail-php-api

1.0.2 2023-11-30 11:34 UTC

This package is auto-updated.

Last update: 2025-03-29 01:07:49 UTC


README

This PHP package is automatically generated by the Swagger Codegen project:

  • API version: v4.0-RM
  • Package version: 1.0.0
  • Build package: io.swagger.codegen.v3.generators.php.PhpClientCodegen

Requirements

PHP 7.4 and later

Installation & Usage

Composer

Then run composer install xi0s/royal-mail-php-api

Tests

To run the unit tests:

composer install
./vendor/bin/phpunit

Getting Started

Please follow the installation procedure and then run the following:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

/**
 * Example OAuth token using League of PHP OAuth client
 */
$provider = new League\OAuth2\Client\Provider\GenericProvider([
  'clientId'                => constant('SHIPPING_API_ROYALMAIL_V4_CLIENT'),    // The client ID assigned to you by the provider
  'clientSecret'            => constant('SHIPPING_API_ROYALMAIL_V4_SECRET'),    // The client password assigned to you by the provider
  'urlAuthorize'            => 'https://authentication.proshipping.net/connect/authorize',
  'urlAccessToken'          => 'https://authentication.proshipping.net/connect/token',
  'urlResourceOwnerDetails' => 'https://authentication.proshipping.net/connect/resource',
]);
try {
  $accessToken = $provider->getAccessToken('client_credentials');
} catch (League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
  throw $e;
}

// Configure OAuth2 access token for authorization: oauth2
$config = RoyalMailV4\ShipmentsApi\Configuration::getDefaultConfiguration()->setAccessToken($accessToken);

$items = [];
$total = 0;
foreach ($packetItems as $row) {
  $total   += round($row['price']*$row['qty'], 2);
  $items[] = new RoyalMailV4\ShipmentsApi\Model\Item([
    'description' => $row['name'],
    'quantity' => $row['qty'],
    'value' => $row['price'],
    'skuCode' => $row['sku'],
  ]);
}

$address = new RoyalMailV4\ShipmentsApi\Model\ShipmentAddress([
  'companyName' => '',
  'contactName' => '',
  'line1' => '',
  'line2' => '',
  'town' => '',
  'county' => '',
  'countryCode' => '',
  'postcode' => '',
  'contactPhone' => '',
  'contactEmail' => '',
]);
$destination = new RoyalMailV4\ShipmentsApi\Model\Destination([
  'address' => $address,
]);

$shipper = new RoyalMailV4\ShipmentsApi\Model\Shipper([
  'shippingAccountId' => constant('SHIPPING_API_ROYALMAIL_V4_SHIPPING_ACCOUNT_ID'), // Your RM shipping ID
  'shippingLocationId' => '', // guid from the v4 shipping console
  'reference1' => '', // your personal reference for the packet
]);

$shipmentInformation = new RoyalMailV4\ShipmentsApi\Model\RoyalMailLabelFormatShipmentInformation([
  'shipmentDate' => new \DateTime('now', new \DateTimeZone('UTC')),
  'serviceCode' => '', // RM Service code
  'declaredWeight' => 1,
  'weightUnitOfMeasure' => RoyalMailV4\ShipmentsApi\Model\WeightUnitOfMeasure::KG,
  'descriptionOfGoods' => '',
  'declaredValue' => (float) $total,
  'currencyCode' => 'GBP',
  'labelFormat' => RoyalMailV4\ShipmentsApi\Model\RoyalMailLabelFormat::PDF, // Label type of PDF
  'action' => RoyalMailV4\ShipmentsApi\Model\CreateShipmentAction::PROCESS, // Automatically process the label
  'contentType' => RoyalMailV4\ShipmentsApi\Model\ContentType::NDX, // Non-documents, for example
]);

$enhancements = [];
$enhancements[] = new RoyalMailV4\ShipmentsApi\Model\RoyalMailServiceEnhancement([
  'code' => RoyalMailV4\ShipmentsApi\Model\RoyalMailEnhancementCode::SAFEPLACE,
  'safeplaceLocation' => '',
]);
/**
 * Ensure that the service code supports these enhancements
 */
$enhancements[] = new RoyalMailV4\ShipmentsApi\Model\RoyalMailServiceEnhancement([
  'code' => RoyalMailV4\ShipmentsApi\Model\RoyalMailEnhancementCode::EMAIL,
]);
$enhancements[] = new RoyalMailV4\ShipmentsApi\Model\RoyalMailServiceEnhancement([
  'code' => RoyalMailV4\ShipmentsApi\Model\RoyalMailEnhancementCode::SMS,
]);
$carrierSpecifics = new RoyalMailV4\ShipmentsApi\Model\RoyalMailShipmentRequestCarrierSpecifics([
  'serviceEnhancements' => $enhancements,
]);

$package = new RoyalMailV4\ShipmentsApi\Model\RoyalMailPackage([
  'packageType' => RoyalMailV4\ShipmentsApi\Model\RoyalMailPackageTypeCode::PARCEL,
  'declaredValue' => (float) $total,
  'declaredWeight' => 1,
]);

$shipment = new RoyalMailV4\ShipmentsApi\Model\RoyalMailShipmentRequestCarrierSpecificsRoyalMailPackageRoyalMailLabelFormatCreateShipmentRequest([
  'shipmentInformation' => $shipmentInformation,
  'shipper' => $shipper,
  'destination' => $destination,
  'items' => $items,
  'carrierSpecifics' => $carrierSpecifics,
  'packages' => [$package],
  // 'customs' => '',
  // 'returnToSender' => '',
]);

try {
  $api = new RoyalMailV4\ShipmentsApi\Api\ShipmentsApi(null, $config);
  $response = $api->v4ShipmentsRmPost($shipment);
} catch (RoyalMailV4\ShipmentsApi\ApiException $e) {
}

// Save the PDF label to disk:
$base64pdf = $response->getLabels();
$data = base64_decode($base64pdf);
$filename = "rmlabel.pdf";
file_put_contents(__DIR__ . "{$filename}.pdf", $data);

// get the shipment ID - used for updates or anything [is now a guid]
$response->getPackages()[0]->getShipmentId();

// tracking number or the numbe that's printed on the label
$response->getPackages()[0]->getTrackingNumber();

// Get the tracking URL
$response->getPackages()[0]->getCarrierTrackingUrl();
?>

Documentation for API Endpoints

All URIs are relative to https://api.proshipping.net/

Class Method HTTP request Description
OfflineBarcodingApi v4OfflineBarcodeNumberRangeRmPost POST /v4/offlineBarcodeNumberRange/rm Get Barcode Range
ShipmentsApi v4ShipmentsDeferPut PUT /v4/shipments/defer Defer Shipments
ShipmentsApi v4ShipmentsPreallocateTrackingNumberRmPost POST /v4/shipments/preallocateTrackingNumber/rm Pre Allocate Tracking Number
ShipmentsApi v4ShipmentsPrintDocumentCarrierCodeShipmentIdPost POST /v4/shipments/printDocument/{carrierCode}/{shipmentId} Print Document
ShipmentsApi v4ShipmentsPrintLabelRmShipmentIdGet GET /v4/shipments/printLabel/rm/{shipmentId} Print Label
ShipmentsApi v4ShipmentsPrintMyLabelQRCodeRmShipmentIdGet GET /v4/shipments/printMyLabelQRCode/rm/{shipmentId} Print My Label QR Code
ShipmentsApi v4ShipmentsRmPost POST /v4/shipments/rm Create Shipment
ShipmentsApi v4ShipmentsShippingLocationIdGet GET /v4/shipments/{shippingLocationId} Get Shipments
ShipmentsApi v4ShipmentsStatusPut PUT /v4/shipments/status Update Status
ShippingAccountsApi v4ShippingAccountsCarrierCodeShippingAccountIdDelete DELETE /v4/shippingAccounts/{carrierCode}/{shippingAccountId} Delete Account
ShippingAccountsApi v4ShippingAccountsCarrierCodeShippingAccountIdUnlinkLocationsPut PUT /v4/shippingAccounts/{carrierCode}/{shippingAccountId}/unlinkLocations Unlink Locations
ShippingAccountsApi v4ShippingAccountsGet GET /v4/shippingAccounts Get Accounts
ShippingAccountsApi v4ShippingAccountsRmGet GET /v4/shippingAccounts/rm Get Carrier Accounts
ShippingAccountsApi v4ShippingAccountsRmPost POST /v4/shippingAccounts/rm Add Account
ShippingAccountsApi v4ShippingAccountsRmShippingAccountIdGet GET /v4/shippingAccounts/rm/{shippingAccountId} Get Account
ShippingAccountsApi v4ShippingAccountsRmShippingAccountIdLinkLocationsPost POST /v4/shippingAccounts/rm/{shippingAccountId}/linkLocations Link Locations
ShippingAccountsApi v4ShippingAccountsRmShippingAccountIdPut PUT /v4/shippingAccounts/rm/{shippingAccountId} Update Account
ShippingAccountsApi v4ShippingAccountsRmShippingAccountIdShippingLocationsGet GET /v4/shippingAccounts/rm/{shippingAccountId}/shippingLocations Get Associated Locations
ShippingAccountsApi v4ShippingAccountsRmShippingAccountIdShippingLocationsShippingLocationIdGet GET /v4/shippingAccounts/rm/{shippingAccountId}/shippingLocations/{shippingLocationId} Get Associated Location
ShippingAccountsApi v4ShippingAccountsRmShippingAccountIdShippingLocationsShippingLocationIdPut PUT /v4/shippingAccounts/rm/{shippingAccountId}/shippingLocations/{shippingLocationId} Update Associated Location

Documentation For Models

Documentation For Authorization

oauth2

  • Type: OAuth
  • Flow: application
  • Authorization URL:
  • Scopes:

Author