5.6.4 Update Shipment Weight (PHP)
Update Shipment Weight
Sample source code to update shipment weight.
<?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/";
$url = $sandbox_url;
$myRefreshToken = "88888888"; // Replace with your Refresh Token
function getAccessToken() {
global $url, $myRefreshToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $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 updateShipmentWeight($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . 'shipment/v2/UpdateWeight');
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 pgwApiDate($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 pgwApiDateNow() {
// 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 (isset($obj['Summary']['Success']) && $obj['Summary']['Success'] > 0) {
// send to the browser
echo $obj['Summary']['Success'] . " shipment weights updated successfully.";
} else if (isset($obj['Summary']['Failed']) && $obj['Summary']['Failed'] > 0) {
foreach($obj['Errors'] as $errors) {
echo "Error: ";
echo $errors['ErrorMessage'] . "<br>";
}
}
}
$data = '
{
"ShipmentsWeight" : [
{
"HawbNo" : "458040010395273",
"ActualWeight" : 3.0
},
{
"HawbNo" : "458040010400335",
"ActualWeight" : 7.0
}
]
}';
$response = updateShipmentWeight($url, $data);
$obj = json_decode($response,true);
parseResponse($obj);
?>