5.1.4 Create Shipments (PHP)
Create Shipment
Sample source code to create shipment
<?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 your Refresh Token here
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 createShipment($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . 'shipment/v2/Create');
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 (isset($obj['Summary']['Success']) && $obj['Summary']['Success'] > 0) {
foreach($obj['ShipmentsResponse'] as $resp) {
if ($resp['IsSuccess']) {
echo "Successful! Tracking Number (HawbNo): ";
echo $resp['ShipmentDetails']['HawbNo'] . "<br>";
}
}
} else if (isset($obj['Summary']['Failed']) && $obj['Summary']['Failed'] > 0) {
foreach($obj['Errors'] as $errors) {
echo "Error: ";
echo $errors['ErrorMessage'] . "<br>";
}
}
}
$data = '
{
"Shipments": [
{
"Shipper": {
"Name": "DEMO ACCOUNT",
"ContactPerson": "Mr Mehmet Abadi",
"Address1": "46-02 JALAN TUN ABDUL RAZAK,",
"Address2": "SUSUR 1,",
"Address3": "",
"Postcode": "80000",
"City": "JOHOR BAHRU",
"State": "JOHOR",
"CountryCode": "MY",
"Phone1": "+6072222668"
},
"Consignee": {
"ContactPerson": "Mr Nick Champ",
"Address1": "Test Address 1",
"Address2": "Test Address 2",
"Postcode": "70000",
"City": "SEREMBAN",
"State": "NEGERI SEMBILAN",
"CountryCode": "MY",
"Phone1": "03-8888888"
},
"Items": [
{
"Description": "SHOES",
"Quantity": 1,
"UnitValue": 20.0,
"HSCode": "64035990",
"SKU": "BC008237",
"Url": "http://shoes.com/?id=123"
}
],
"Packages": [
{
"PackageReference": "PKG001",
"Length": 30.0,
"Width": 10.0,
"Height": 10.0,
"ActualWeight": 3.5
}
],
"PackageType": "SPX",
"WeightType": "KG",
"ShipmentDate": "' . prinshipApiDateNow() . '",
"TOSMode": "MY-E-LWE",
"ReferenceNo": "ORDERREF00001",
"CurrencyCode": "MYR",
"CODValue": 0,
"CODCurrency": ""
"InsuranceValue": 10.0,
"InsuranceCurrency": "MYR"
}
]
}';
echo "Request:<br>" . $data . "<br><br>";
$response = createShipment($sandbox_url, $data);
$obj = json_decode($response,true);
echo $response . "<br><br>";
parseResponse($obj);
?>