royal mail tracking api
1.0.2
為了能夠通過Royal Mail API跟踪項目,您需要成為皇家郵件帳戶持有人,並通過https://developer.royalmail.net/批准通過Royal Mail開發人員Portal訪問跟踪API
composer require nomisoft/royal-mail-tracking-api
您需要您希望檢索的項目的跟踪參考編號以及客戶ID,客戶秘密和應用ID(全部可從Royal Mail開發人員Portal中獲得)
use Nomisoft RoyalMailTrackingApi RoyalMail ;
use Nomisoft RoyalMailTrackingApi RoyalMailException ;
$ rm = RoyalMail :: init ( ' CLIENT_ID ' , ' CLIENT_SECRET ' , ' APP_ID ' );
try {
$ response = $ rm -> getSingleItemSummary ( ' XX12345678XX ' );
} catch ( SoapFault $ e ) {
echo $ e -> getMessage ();
} catch ( RoyalMailException $ e ) {
echo $ e -> getMessage () . ' - ' . $ e -> getCause ();
}
返回的響應是一個包含DateTime對象的數組,狀態和摘要文本
Array
(
[datetime] => DateTime Object
(
[date] => 2016 - 06 - 01 12 : 30 : 00
[timezone_type] => 3
[timezone] => Europe / London
)
[status] => Delivered
[summary] => Item XX12345678XX was collected and signed for by the addressee on the 2016 - 06 - 01 from Kidderminster DO .
)
use Nomisoft RoyalMailTrackingApi RoyalMail ;
$ rm = RoyalMail :: init ( ' CLIENT_ID ' , ' CLIENT_SECRET ' , ' APP_ID ' );
$ response = $ rm -> getSingleItemHistory ( ' XX12345678XX ' );
返回的響應是多維陣列。每個交貨狀態更新都包含DateTime對象,位置和狀態
Array
(
[ 0 ] => Array
(
[datetime] => DateTime Object
(
[date] => 2016 - 06 - 01 12 : 30 : 00
[timezone_type] => 3
[timezone] => Europe / London
)
[location] => Kidderminster DO
[status] => Delivered
)
[ 1 ] => Array
(
[datetime] => DateTime Object
(
[date] => 2016 - 05 - 31 15 : 30 : 00
[timezone_type] => 3
[timezone] => Europe / London
)
[location] => Kidderminster DO
[status] => We have your item
)
[ 2 ] => Array
(
[datetime] => DateTime Object
(
[date] => 2016 - 05 - 31 12 : 30 : 00
[timezone_type] => 3
[timezone] => Europe / London
)
[location] => Kidderminster DO
[status] => Delivery attempted
)
[ 3 ] => Array
(
[datetime] => DateTime Object
(
[date] => 2016 - 05 - 28 10 : 00 : 00
[timezone_type] => 3
[timezone] => Europe / London
)
[location] => Medway Mail Centre
[status] => It 's on its way.
)
)
use Nomisoft RoyalMailTrackingApi RoyalMail ;
$ rm = RoyalMail :: init ( ' CLIENT_ID ' , ' CLIENT_SECRET ' , ' APP_ID ' );
$ response = $ rm -> getMultiItemSummary ( array ( ' XX12345678XX ' , ' ZZ12345678ZZ ' ));
返回的響應是一個多維數組,其跟踪參考號作為鍵。每個項目都包含DateTime對象,狀態和摘要
Array
(
[ XX12345678XX ] => Array
(
[datetime] => DateTime Object
(
[date] => 2016 - 06 - 01 12 : 30 : 00
[timezone_type] => 3
[timezone] => Europe / London
)
[status] => Delivered
[summary] => Item XX12345678XX was collected and signed for by the addressee on the 2016 - 06 - 01 from Kidderminster DO .
)
[ ZZ12345678ZZ ] => Array
(
[datetime] => DateTime Object
(
[date] => 2016 - 06 - 01 12 : 30 : 00
[timezone_type] => 3
[timezone] => Europe / London
)
[status] => Delivered
[summary] => We have a record of item ZZ12345678ZZ as being delivered from Fort William DO on 2016 - 06 - 01.
)
)
use Nomisoft RoyalMailTrackingApi RoyalMail ;
$ rm = RoyalMail :: init ( ' CLIENT_ID ' , ' CLIENT_SECRET ' , ' APP_ID ' );
$ response = $ rm -> getProofOfDelivery ( ' XX12345678XX ' );
返回的響應是一個包含DateTime對象的數組和簽名包裹的人的名稱
Array
(
[datetime] => DateTime Object
(
[date] => 2016 - 06 - 01 12 : 30 : 00
[timezone_type] => 3
[timezone] => Europe / London
)
[name] => Smith
)
如果從API收到錯誤響應,則會引發RoyalMailMailException例外。調用getMessage()函數將返回皇家郵件的錯誤描述。還有一個額外的getCause()函數,它將返回API返回的“原因”字段的值
try {
$ response = $ rm -> getSingleItemSummary ( ' XX12345678XX ' );
} catch ( RoyalMailException $ e ) {
echo $ e -> getMessage ();
echo $ e -> getCause ();
}
如果該物品在30天前交付
Tracking data are not available for barcode reference XX12345678XX
It is not possible to provide information about events that occurred more than 30 days ago