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 17Track
|
||||
$apiKey = '489F6B6DADDE09A5B6CB1C42B5363A3F'; // Sostituisci con 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 17Track
|
||||
function makeRequest($url, $data, $apiKey, $method = 'POST')
|
||||
{
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'17token: ' . $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) {
|
||||
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)'
|
||||
];
|
||||
}
|
||||
|
||||
return $decodedResponse;
|
||||
}
|
||||
|
||||
// Step 1: Prova con auto-detection
|
||||
$trackUrl = 'https://api.17track.net/track/v2/register';
|
||||
$trackData = [
|
||||
[
|
||||
'number' => $trackingNumber,
|
||||
'carrier' => null,
|
||||
'auto_detection' => true
|
||||
]
|
||||
];
|
||||
$registerResponse = makeRequest($trackUrl, $trackData, $apiKey);
|
||||
|
||||
file_put_contents('debug.log', "Register Response (Auto-detect): " . json_encode($registerResponse) . "\n", FILE_APPEND);
|
||||
|
||||
if (!isset($registerResponse['data']['accepted']) || empty($registerResponse['data']['accepted'])) {
|
||||
$errorMessage = $registerResponse['data']['rejected'][0]['error']['message'] ?? 'Errore sconosciuto';
|
||||
sendResponse(['success' => false, 'message' => 'Errore nella registrazione del tracking: ' . $errorMessage]);
|
||||
}
|
||||
|
||||
// Step 2: Recupera i dettagli con riprova
|
||||
$getUrl = 'https://api.17track.net/track/v2/gettrackinfo';
|
||||
$getData = [['number' => $trackingNumber]];
|
||||
$maxAttempts = 3;
|
||||
$delaySeconds = 5;
|
||||
|
||||
for ($attempt = 1; $attempt <= $maxAttempts; $attempt++) {
|
||||
$trackResponse = makeRequest($getUrl, $getData, $apiKey);
|
||||
file_put_contents('debug.log', "Track Response (Attempt $attempt): " . json_encode($trackResponse) . "\n", FILE_APPEND);
|
||||
|
||||
if (isset($trackResponse['data']['accepted']) && !empty($trackResponse['data']['accepted'])) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ($attempt < $maxAttempts) {
|
||||
sleep($delaySeconds);
|
||||
}
|
||||
}
|
||||
|
||||
// Se auto-detection ha successo, procedi
|
||||
if (isset($trackResponse['data']['accepted']) && !empty($trackResponse['data']['accepted'])) {
|
||||
$trackingInfo = $trackResponse['data']['accepted'][0]['track'] ?? null;
|
||||
if (!$trackingInfo) {
|
||||
sendResponse(['success' => false, 'message' => 'Nessuna informazione di tracking trovata']);
|
||||
}
|
||||
|
||||
$deliveryDate = 'Non disponibile';
|
||||
$signedBy = 'Non disponibile';
|
||||
$carrierName = $trackingInfo['carrier']['name'] ?? 'Sconosciuto';
|
||||
|
||||
if (isset($trackingInfo['z0']['e']) && $trackingInfo['z0']['e'] == 40) {
|
||||
$deliveryDate = $trackingInfo['z0']['z'] ?? 'Non disponibile';
|
||||
$signedBy = $trackingInfo['z0']['d'] ?? 'Non disponibile';
|
||||
}
|
||||
|
||||
sendResponse([
|
||||
'success' => true,
|
||||
'deliveryDate' => $deliveryDate,
|
||||
'signedBy' => $signedBy,
|
||||
'carrierName' => $carrierName
|
||||
]);
|
||||
}
|
||||
|
||||
// Step 3: Se auto-detection fallisce, prova una lista di corrieri comuni
|
||||
$commonCarriers = [
|
||||
['code' => 100003, 'name' => 'TNT'], // TNT
|
||||
['code' => 100001, 'name' => 'DHL'], // DHL Express
|
||||
['code' => 100065, 'name' => 'DHL eCommerce'], // DHL eCommerce
|
||||
['code' => 100002, 'name' => 'UPS'] // UPS
|
||||
];
|
||||
|
||||
$possibleCarriers = [];
|
||||
foreach ($commonCarriers as $carrier) {
|
||||
$trackData = [
|
||||
[
|
||||
'number' => $trackingNumber,
|
||||
'carrier' => $carrier['code'],
|
||||
'auto_detection' => false
|
||||
]
|
||||
];
|
||||
$registerResponse = makeRequest($trackUrl, $trackData, $apiKey);
|
||||
|
||||
if (isset($registerResponse['data']['accepted']) && !empty($registerResponse['data']['accepted'])) {
|
||||
$possibleCarriers[] = $carrier['name'];
|
||||
}
|
||||
sleep(1); // Piccolo ritardo per evitare limiti di rate
|
||||
}
|
||||
|
||||
if (!empty($possibleCarriers)) {
|
||||
sendResponse([
|
||||
'success' => false,
|
||||
'message' => 'Auto-detection fallita. Seleziona un corriere tra quelli possibili.',
|
||||
'possibleCarriers' => $possibleCarriers
|
||||
]);
|
||||
} else {
|
||||
sendResponse(['success' => false, 'message' => 'Nessun corriere identificato per questo numero di tracking']);
|
||||
}
|
||||
Reference in New Issue
Block a user