5.2.4 Modify Shipments (PHP)
Modify Shipment
Sample source code to modify shipment. Please note that a new HAWB will be generated.
<?php
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set("Asia/Chongqing");
$production_url = "https://apiv2.unixus.com.my/";
$sandbox_url = "https://sandbox-apiv2.unixus.com.my/";
$myRefreshToken = "88888888";
function getAccessToken() {
global $sandbox_url, $myRefreshToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sandbox_url . 'Token/Refresh');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept-Language: en'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "RefreshToken" : "' . $myRefreshToken . '"}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res = curl_exec($ch);
curl_close($ch);
$obj = json_decode($res,true);
return $obj['AccessToken'];
}
function modifyShipment($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . 'shipment/v2/Modify');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept-Language: en',
'Authorization: Bearer ' .
getAccessToken()
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
function prinshipApiDate($myDate) {
// input "2019-01-01 00:00:00"
// output "2019-01-01T00:00:00+08:00"
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $myDate);
$requiredJsonFormat = $dateTime->format('Y-m-d\TH:i:sP');
return $requiredJsonFormat;
}
function prinshipApiDateNow() {
// output "2019-01-01T00:00:00+08:00"
$dateTime = new DateTime();
$requiredJsonFormat = $dateTime->format('Y-m-d\TH:i:sP');
return $requiredJsonFormat;
}
function parseResponse($obj) {
if ($obj['Summary']['Success'] > 0) {
foreach($obj['ShipmentsResponse'] as $resp) {
if ($resp['IsSuccess']) {
echo "Successful: ";
echo $resp['ShipmentDetails']['HawbNo'] . "<br>";
}
}
}
if ($obj['Summary']['Failed'] > 0) {
foreach($obj['Errors'] as $errors) {
echo "Error: ";
echo $errors['ErrorMessage'] . "<br>";
}
}
}
$data = '
{
"Shipments": [
{
"Shipper": {
"Name": "DEMO ACCOUNT",
"ContactPerson": "Ms Angie",
"Address1": "No.4",
"Address2": "Jalan SS13/6C",
"Address3": "",
"Postcode": "47500",
"City": "Subang Jaya",
"State": "Selangor",
"CountryCode": "MY",
"Phone1": "+60388008830"
},
"Consignee": {
"ContactPerson": "James Brown",
"Address1": "Test Address 1",
"Address2": "Test Address 2",
"Postcode": "70000",
"City": "JOHOR BAHRU",
"State": "SELANGOR",
"CountryCode": "MY",
"Phone1": "012-3668899"
},
"Items": [
{
"Description": "Shoes, TShirt",
"Quantity": 2,
"UnitValue": 50,
"HSCode": "64035990",
"SKU": "ABC123",
"Url": "http://www.mywebsite.com"
}
],
"Packages": [
{
"PackageReference": "Shoes001",
"Length": 30,
"Width": 10,
"Height": 10,
"ActualWeight": 1.5
},
{
"PackageReference": "TShirt002",
"Length": 30,
"Width": 30,
"Height": 10,
"ActualWeight": 1.1
}
],
"HawbNo": "458040010619601",
"PackageType": "SPX",
"WeightType": "KG",
"ShipmentDate": "' . prinshipApiDateNow() . '",
"TOSMode": "MY-E-LWE",
"ReferenceNo": "ORDERREF00001",
"CurrencyCode": "MYR"
}
]
}';
echo "Request:<br>" . $data . "<br><br>";
$response = modifyShipment($sandbox_url, $data);
echo $response . "<br><br>";
$obj = json_decode($response,true);
parseResponse($obj);
?>