MNG Kargo API
MNG Kargo, REST tabanlı modern bir API sunar. JSON formatında veri alışverişi yapılır. Gönderi oluşturma, barkod alma, kargo takibi ve fiyat hesaplama işlemlerini destekler.
API Bilgileri
- Base URL: https://api.mngkargo.com.tr/mngapi/api
- Auth: Bearer Token (API Key)
1. Token Alma
function mng_get_token($api_key, $api_secret) {
$ch = curl_init('https://api.mngkargo.com.tr/token');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query([
'grant_type' => 'password',
'username' => $api_key,
'password' => $api_secret,
]),
CURLOPT_RETURNTRANSFER => true,
]);
$res = json_decode(curl_exec($ch));
curl_close($ch);
return $res->access_token;
}
2. Gönderi Oluşturma
function mng_create_shipment($token, $order) {
$payload = json_encode([
'order' => [
'referenceId' => $order['id'],
'barcode' => '',
'billOfLandingId' => '',
'isCOD' => 0,
'codAmount' => 0,
'packagingType'=> 1,
'content' => $order['product'],
'count' => 1,
'weight' => $order['weight'],
'volume' => $order['desi'],
'recipient' => [
'customerId' => '',
'firstName' => $order['name'],
'phone' => $order['phone'],
'cityCode' => $order['city'],
'districtName'=> $order['district'],
'address' => $order['address'],
],
],
]);
$ch = curl_init('https://api.mngkargo.com.tr/mngapi/api/standardshipment');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $token,
'Content-Type: application/json',
],
CURLOPT_RETURNTRANSFER => true,
]);
return json_decode(curl_exec($ch));
}