PHP code example of elliotjreed / royal-mail-tracking
1. Go to this page and download the library: Download elliotjreed/royal-mail-tracking library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
elliotjreed / royal-mail-tracking example snippets
$tracking = (new \ElliotJReed\RoyalMail\Tracking\Events(
new \GuzzleHttp\Client(),
'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
'12345678901234567890123456789012345678901234567890',
true, // Optional, when true (default: true) exceptions will be thrown for tracking errors
true, // Optional, when true (default: true) exceptions will be thrown for technical (eg. 500 HTTP response) errors
'https://api.royalmail.net/mailpieces/v2' // Optional, when set the default API endpoint can be overridden (default: 'https://api.royalmail.net/mailpieces/v2')
));
$signature = (new \ElliotJReed\RoyalMail\Tracking\Signature(
new \GuzzleHttp\Client(),
'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
'12345678901234567890123456789012345678901234567890',
true, // Optional, when true (default: true) exceptions will be thrown for tracking errors
true, // Optional, when true (default: true) exceptions will be thrown for technical (eg. 500 HTTP response) errors
'https://api.royalmail.net/mailpieces/v2' // Optional, when set the default API endpoint can be overridden (default: 'https://api.royalmail.net/mailpieces/v2')
));
$summary = (new \ElliotJReed\RoyalMail\Tracking\Summary(
new \GuzzleHttp\Client(),
'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
'12345678901234567890123456789012345678901234567890',
true, // Optional, when true (default: true) exceptions will be thrown for tracking errors
true, // Optional, when true (default: true) exceptions will be thrown for technical (eg. 500 HTTP response) errors
'https://api.royalmail.net/mailpieces/v2' // Optional, when set the default API endpoint can be overridden (default: 'https://api.royalmail.net/mailpieces/v2')
));
$tracking = (new \ElliotJReed\RoyalMail\Tracking\Events(
new \GuzzleHttp\Client(),
'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
'12345678901234567890123456789012345678901234567890'
));
$mailPieces = $tracking->setTrackingNumber('AB1234567890GB')->getResponse()->getMailPieces();
$mailPieces->getMailPieceId(); // 090367574000000FE1E1B
$mailPieces->getCarrierShortName(); // RM
$mailPieces->getCarrierFullName(); // Royal Mail Group Ltd
$summary = $mailPieces->getSummary();
$summary->getUniqueItemId(); // 090367574000000FE1E1B
$summary->getOneDBarcode(); // FQ087430672GB
$summary->getProductId(); // SD2
$summary->getProductName(); // Special Delivery Guaranteed
$summary->getProductDescription(); // Our guaranteed next day service with tracking and a signature on delivery
$summary->getProductCategory(); // NON-INTERNATIONAL
$summary->getDestinationCountryCode(); // GBR
$summary->getDestinationCountryName(); // United Kingdom of Great Britain and Northern Ireland
$summary->getOriginCountryCode(); // GBR
$summary->getOriginCountryName(); // United Kingdom of Great Britain and Northern Ireland
$summary->getLastEventCode(); // EVNMI
$summary->getLastEventName(); // Forwarded - Mis-sort
$summary->getLastEventDateTime(); // new DateTimeImmutable('2016-10-20T10:04:00+01:00')
$summary->getLastEventLocationName(); // Stafford DO
$summary->getStatusDescription(); // It is being redirected
$summary->getStatusCategory(); // IN TRANSIT
$summary->getStatusHelpText(); // The item is in transit
$summary->getSummaryLine(); // Item FQ087430672GB was forwarded to the Delivery Office on 2016-10-20.
$internationalPostalProvider = $summary->getInternationalPostalProvider();
$internationalPostalProvider->getUrl(); // https://www.royalmail.com/track-your-item
$internationalPostalProvider->getTitle(); // Royal Mail Group Ltd
$internationalPostalProvider->getDescription(); // Royal Mail Group Ltd
$signature = $mailPieces->getSignature();
$signature->getRecipientName(); // Elliot
$signature->getSignatureDateTime(); // new DateTimeImmutable('2016-10-20T10:04:00+01:00')
$signature->getImageId(); // 001234
$estimatedDelivery = $mailPieces->getEstimatedDelivery();
$estimatedDelivery->getDate(); // new DateTimeImmutable('2017-02-20T00:00:00+00:00')
$estimatedDelivery->getStartOfEstimatedWindow(); // new DateTimeImmutable('2017-02-20T08:00:00+01:00')
$estimatedDelivery->getEndOfEstimatedWindow(); // new DateTimeImmutable('2017-02-20T11:00:00+01:00')
$events = $mailPieces->getEvents();
$event = $events[0];
$event->getEventCode(); // EVNMI
$event->getEventName(); // Forwarded - Mis-sort
$event->getEventDateTime(); // new DateTimeImmutable('2016-10-20T10:04:00+01:00')
$event->getLocationName(); // Stafford DO
$linkSummary = $mailPieces->getLinks()->getSummary();
$linkSummary->getHref(); // /mailpieces/v2/summary?mailPieceId=090367574000000FE1E1B
$linkSummary->getTitle(); // Summary
$linkSummary->getDescription(); // Get summary
$linkSignature = $mailPieces->getLinks()->getSignature();
$linkSignature->getHref(); // /mailpieces/v2/090367574000000FE1E1B/signature
$linkSignature->getTitle(); // Signature
$linkSignature->getDescription(); // Get signature
$linkRedelivery = $mailPieces->getLinks()->getRedelivery();
$linkRedelivery->getHref(); // /personal/receiving-mail/redelivery
$linkRedelivery->getTitle(); // Redelivery
$linkRedelivery->getDescription(); // Book a redelivery
$tracking = (new \ElliotJReed\RoyalMail\Tracking\Events(
new \GuzzleHttp\Client(),
'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
'12345678901234567890123456789012345678901234567890'
));
echo $tracking->setTrackingNumber('AB1234567890GB')->asJson();
$tracking = (new \ElliotJReed\RoyalMail\Tracking\Signature(
new \GuzzleHttp\Client(),
'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
'12345678901234567890123456789012345678901234567890'
));
echo $tracking->setTrackingNumber('AB1234567890GB')->asJson();
$summary = (new \ElliotJReed\RoyalMail\Tracking\Summary(
new \GuzzleHttp\Client(),
'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
'12345678901234567890123456789012345678901234567890'
));
$mailPieces = $summary->setTrackingNumbers('AB1234567890GB', 'CD1234567890GB')->getResponse()->getMailPieces();
$firstMailPieces = $mailPieces[0];
$firstMailPieces->getMailPieceId(); // 090367574000000FE1E1B
$firstMailPieces->getCarrierShortName(); // RM
$firstMailPieces->getCarrierFullName(); // Royal Mail Group Ltd
$summary = $firstMailPieces->getSummary();
$summary->getUniqueItemId(); // 090367574000000FE1E1B
$summary->getOneDBarcode(); // FQ087430672GB
$summary->getProductId(); // SD2
$summary->getProductName(); // Special Delivery Guaranteed
$summary->getProductDescription(); // Our guaranteed next day service with tracking and a signature on delivery
$summary->getProductCategory(); // NON-INTERNATIONAL
$summary->getDestinationCountryCode(); // GBR
$summary->getDestinationCountryName(); // United Kingdom of Great Britain and Northern Ireland
$summary->getOriginCountryCode(); // GBR
$summary->getOriginCountryName(); // United Kingdom of Great Britain and Northern Ireland
$summary->getLastEventCode(); // EVNMI
$summary->getLastEventName(); // Forwarded - Mis-sort
$summary->getLastEventDateTime(); // new DateTimeImmutable('2016-10-20T10:04:00+01:00')
$summary->getLastEventLocationName(); // Stafford DO
$summary->getStatusDescription(); // It is being redirected
$summary->getStatusCategory(); // IN TRANSIT
$summary->getStatusHelpText(); // The item is in transit
$summary->getSummaryLine(); // Item FQ087430672GB was forwarded to the Delivery Office on 2016-10-20.
$internationalPostalProvider = $summary->getInternationalPostalProvider();
$internationalPostalProvider->getUrl(); // https://www.royalmail.com/track-your-item
$internationalPostalProvider->getTitle(); // Royal Mail Group Ltd
$internationalPostalProvider->getDescription(); // Royal Mail Group Ltd
$events = $firstMailPieces->getLinks()->getEvents();
$events->getHref(); // /mailpieces/v2/FQ087430672GB/events
$events->getTitle(); // Events
$events->getDescription(); // Get events
$error = $firstMailPieces->getError();
$error->getErrorCode(); // E1142
$error->getErrorDescription(); // Barcode reference $mailPieceId isn't recognised
$error->getErrorCause(); // A mail item with that barcode cannot be located
$error->getErrorResolution(); // Check barcode and resubmit
$summary = (new \ElliotJReed\RoyalMail\Tracking\Summary(
new \GuzzleHttp\Client(),
'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
'12345678901234567890123456789012345678901234567890'
));
echo $summary->asJson();