added photo functionality
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
// Abilita il debug degli errori (solo per sviluppo)
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
// Assicurati che non ci sia output prima del JSON
|
||||
ob_start();
|
||||
|
||||
// Imposta l'header per JSON
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Configura la tua chiave API di TrackingMore
|
||||
$apiKey = 'u4ssgynn-xuyy-9act-ca29-2glsv2qlzh0w'; // La tua chiave API reale
|
||||
|
||||
// Funzione per inviare una risposta JSON e terminare l'esecuzione
|
||||
function sendResponse($data)
|
||||
{
|
||||
ob_end_clean();
|
||||
echo json_encode($data);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Verifica che il numero di tracking e il corriere siano stati inviati
|
||||
if (!isset($_POST['tracking_number']) || empty($_POST['tracking_number']) || !isset($_POST['courier_code']) || empty($_POST['courier_code'])) {
|
||||
sendResponse(['success' => false, 'message' => 'Numero di tracking o corriere non fornito']);
|
||||
}
|
||||
|
||||
$trackingNumber = $_POST['tracking_number'];
|
||||
$courierCode = $_POST['courier_code'];
|
||||
|
||||
// Lista dei corrieri validi per validazione
|
||||
$validCarriers = ['tnt-it', 'dhl', 'gls', 'sda', 'ups'];
|
||||
if (!in_array($courierCode, $validCarriers)) {
|
||||
sendResponse(['success' => false, 'message' => 'Corriere non valido']);
|
||||
}
|
||||
|
||||
// Imposta il nome del corriere in base al codice
|
||||
$carrierNames = [
|
||||
'tnt-it' => 'TNT Italy',
|
||||
'dhl' => 'DHL',
|
||||
'gls' => 'GLS',
|
||||
'sda' => 'SDA',
|
||||
'ups' => 'UPS'
|
||||
];
|
||||
$courierName = $carrierNames[$courierCode];
|
||||
|
||||
// Funzione per fare una richiesta cURL a TrackingMore
|
||||
function makeRequest($url, $data, $apiKey, $method = 'POST')
|
||||
{
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Tracking-Api-Key: ' . $apiKey,
|
||||
'Content-Type: application/json'
|
||||
]);
|
||||
if ($method === 'POST') {
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
$jsonData = json_encode($data, JSON_PRETTY_PRINT);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
|
||||
file_put_contents('debug.log', "Encoded JSON Data: $jsonData\n", FILE_APPEND);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
file_put_contents('debug.log', "Request URL: $url\nRequest Data: " . json_encode($data) . "\nResponse: $response\nHTTP Code: $httpCode\nError: $error\n", FILE_APPEND);
|
||||
|
||||
if ($response === false) {
|
||||
return [
|
||||
'success' => false,
|
||||
'code' => $httpCode,
|
||||
'message' => 'Errore nella richiesta API: ' . $error
|
||||
];
|
||||
}
|
||||
|
||||
$decodedResponse = json_decode($response, true);
|
||||
if ($decodedResponse === null) {
|
||||
return [
|
||||
'success' => false,
|
||||
'code' => $httpCode,
|
||||
'message' => 'Risposta API non valida (non è JSON)'
|
||||
];
|
||||
}
|
||||
|
||||
$decodedResponse['success'] = isset($decodedResponse['meta']['code']) && ($decodedResponse['meta']['code'] === 200 || $decodedResponse['meta']['code'] === 201);
|
||||
$decodedResponse['http_code'] = $httpCode;
|
||||
return $decodedResponse;
|
||||
}
|
||||
|
||||
// Step 1: Prova a creare il tracking
|
||||
$createUrl = 'https://api.trackingmore.com/v4/trackings/create';
|
||||
$createData = [
|
||||
'tracking_number' => $trackingNumber,
|
||||
'courier_code' => $courierCode
|
||||
];
|
||||
$createResponse = makeRequest($createUrl, $createData, $apiKey);
|
||||
file_put_contents('debug.log', "Create Response: " . json_encode($createResponse) . "\n", FILE_APPEND);
|
||||
|
||||
$trackingInfo = null;
|
||||
if ($createResponse['success']) {
|
||||
// Creazione riuscita, usa i dati restituiti
|
||||
$trackingInfo = $createResponse['data'];
|
||||
} else {
|
||||
// Controlla se l'errore è "Tracking No. already exists" (4101)
|
||||
if (isset($createResponse['meta']['code']) && $createResponse['meta']['code'] === 4101) {
|
||||
// Il tracking esiste già, usa /trackings (GET) con tracking_number e courier_code
|
||||
$getUrl = 'https://api.trackingmore.com/v4/get?tracking_numbers=' . urlencode($trackingNumber);
|
||||
$getResponse = makeRequest($getUrl, [], $apiKey, 'GET');
|
||||
file_put_contents('debug.log', "Get Response: " . json_encode($getResponse) . "\n", FILE_APPEND);
|
||||
|
||||
if ($getResponse['success'] && !empty($getResponse['data']['items']) && !empty($getResponse['data']['items'][0])) {
|
||||
$trackingInfo = $getResponse['data']['items'][0];
|
||||
} else {
|
||||
$errorMessage = isset($getResponse['meta']['message']) ? $getResponse['meta']['message'] : 'Errore sconosciuto';
|
||||
sendResponse(['success' => false, 'message' => 'Errore nel recupero del tracking esistente: ' . $errorMessage]);
|
||||
}
|
||||
} else {
|
||||
// Altro errore nella creazione
|
||||
$errorMessage = isset($createResponse['meta']['message']) ? $createResponse['meta']['message'] : 'Errore sconosciuto';
|
||||
sendResponse(['success' => false, 'message' => 'Errore nella creazione del tracking: ' . $errorMessage]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$trackingInfo) {
|
||||
sendResponse(['success' => false, 'message' => 'Nessuna informazione di tracking trovata']);
|
||||
}
|
||||
|
||||
// Estrai la data di consegna e il firmatario
|
||||
$deliveryDate = 'Non disponibile';
|
||||
$signedBy = 'Non disponibile';
|
||||
if (isset($trackingInfo['origin_info']['trackinfo']) && is_array($trackingInfo['origin_info']['trackinfo'])) {
|
||||
foreach ($trackingInfo['origin_info']['trackinfo'] as $checkpoint) {
|
||||
if (isset($checkpoint['checkpoint_delivery_status']) && $checkpoint['checkpoint_delivery_status'] === 'delivered') {
|
||||
$deliveryDate = $checkpoint['checkpoint_date'] ?? 'Non disponibile';
|
||||
$signedBy = $trackingInfo['signed_by'] ?? 'Non disponibile';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Restituisci i dati al frontend
|
||||
sendResponse([
|
||||
'success' => true,
|
||||
'deliveryDate' => $deliveryDate,
|
||||
'signedBy' => $signedBy,
|
||||
'carrierName' => $courierName
|
||||
]);
|
||||
Reference in New Issue
Block a user