added photo functionality
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
<?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 sia stato inviato
|
||||
if (!isset($_POST['tracking_number']) || empty($_POST['tracking_number'])) {
|
||||
sendResponse(['success' => false, 'message' => 'Numero di tracking non fornito']);
|
||||
}
|
||||
|
||||
$trackingNumber = $_POST['tracking_number'];
|
||||
|
||||
// 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);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
}
|
||||
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 || ($httpCode !== 200 && $httpCode !== 201)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'code' => $httpCode,
|
||||
'message' => 'Errore nella richiesta API: HTTP ' . $httpCode . ' - ' . $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);
|
||||
return $decodedResponse;
|
||||
}
|
||||
|
||||
// Step 1: Identifica il corriere
|
||||
$detectUrl = 'https://api.trackingmore.com/v4/couriers/detect';
|
||||
$detectData = ['tracking_number' => $trackingNumber];
|
||||
$detectResponse = makeRequest($detectUrl, $detectData, $apiKey);
|
||||
file_put_contents('debug.log', "Detect Response: " . json_encode($detectResponse) . "\n", FILE_APPEND);
|
||||
|
||||
if (!$detectResponse['success']) {
|
||||
$errorMessage = isset($detectResponse['meta']['message']) ? $detectResponse['meta']['message'] : 'Errore sconosciuto';
|
||||
sendResponse(['success' => false, 'message' => 'Errore nella rilevazione del corriere: ' . $errorMessage]);
|
||||
}
|
||||
|
||||
$couriers = $detectResponse['data'] ?? [];
|
||||
if (empty($couriers)) {
|
||||
sendResponse(['success' => false, 'message' => 'Corriere non identificato per il numero di tracking']);
|
||||
}
|
||||
|
||||
// Prendi il primo corriere per ora
|
||||
$courierCode = $couriers[0]['courier_code'] ?? null;
|
||||
$courierName = $couriers[0]['courier_name'] ?? 'Sconosciuto';
|
||||
if (!$courierCode) {
|
||||
sendResponse(['success' => false, 'message' => 'Corriere non identificato']);
|
||||
}
|
||||
|
||||
// Step 2: Crea un 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);
|
||||
|
||||
if (!$createResponse['success']) {
|
||||
$errorMessage = isset($createResponse['meta']['message']) ? $createResponse['meta']['message'] : 'Errore sconosciuto';
|
||||
sendResponse(['success' => false, 'message' => 'Errore nella creazione del tracking: ' . $errorMessage]);
|
||||
}
|
||||
|
||||
// Step 3: Recupera i dettagli del tracking con riprova
|
||||
$getUrl = 'https://api.trackingmore.com/v4/trackings/get?tracking_number=' . urlencode($trackingNumber);
|
||||
$maxAttempts = 3;
|
||||
$delaySeconds = 5;
|
||||
|
||||
for ($attempt = 1; $attempt <= $maxAttempts; $attempt++) {
|
||||
$trackResponse = makeRequest($getUrl, [], $apiKey, 'GET');
|
||||
file_put_contents('debug.log', "Track Response (Attempt $attempt): " . json_encode($trackResponse) . "\n", FILE_APPEND);
|
||||
|
||||
if ($trackResponse['success'] && !empty($trackResponse['data'])) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ($attempt < $maxAttempts) {
|
||||
sleep($delaySeconds);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$trackResponse['success']) {
|
||||
$errorMessage = isset($trackResponse['meta']['message']) ? $trackResponse['meta']['message'] : 'Errore sconosciuto';
|
||||
sendResponse(['success' => false, 'message' => 'Errore nel recupero delle informazioni: ' . $errorMessage]);
|
||||
}
|
||||
|
||||
$trackingInfo = $trackResponse['data'] ?? null;
|
||||
if (!$trackingInfo) {
|
||||
// Se ci sono più corrieri rilevati, restituisci una lista per la tendina
|
||||
if (count($couriers) > 1) {
|
||||
$possibleCarriers = array_map(function ($courier) {
|
||||
return ['code' => $courier['courier_code'], 'name' => $courier['courier_name']];
|
||||
}, $couriers);
|
||||
sendResponse([
|
||||
'success' => false,
|
||||
'message' => 'Dati non trovati. Seleziona un corriere tra quelli rilevati.',
|
||||
'possibleCarriers' => $possibleCarriers
|
||||
]);
|
||||
}
|
||||
sendResponse(['success' => false, 'message' => 'Nessuna informazione di tracking trovata']);
|
||||
}
|
||||
|
||||
// Estrai la data di consegna e il firmatario
|
||||
$deliveryDate = 'Non disponibile';
|
||||
$signedBy = 'Non disponibile';
|
||||
foreach ($trackingInfo['trackinfo'] as $checkpoint) {
|
||||
if ($checkpoint['status'] === 'delivered') {
|
||||
$deliveryDate = $checkpoint['Date'];
|
||||
$signedBy = $checkpoint['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