64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
// 1. Validar parámetros de entrada
|
|
$orderNumber = $_GET['orderNumber'] ?? null;
|
|
$orderCode = $_GET['orderCode'] ?? null;
|
|
|
|
if (!$orderNumber || !$orderCode) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Número de orden y código de orden son requeridos.']);
|
|
exit;
|
|
}
|
|
|
|
// 2. Configurar la llamada a la API de Shalom
|
|
$apiKey = 'sk_mlq1j3na_4a676ewvaop';
|
|
$url = "https://shalom-api.lat/api/track"; // Endpoint correcto para POST
|
|
|
|
// Datos para el cuerpo de la solicitud POST
|
|
$postData = [
|
|
'orderNumber' => $orderNumber,
|
|
'orderCode' => $orderCode
|
|
];
|
|
$jsonData = json_encode($postData);
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true); // Especificar que es una solicitud POST
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); // Enviar los datos en formato JSON
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: application/json',
|
|
'Accept: application/json',
|
|
"Authorization: Bearer {$apiKey}"
|
|
]);
|
|
|
|
// 3. Ejecutar la llamada y obtener la respuesta
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
// 4. Manejar la respuesta
|
|
if ($error) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Error en la comunicación con la API de Shalom: ' . $error]);
|
|
exit;
|
|
}
|
|
|
|
if ($httpCode >= 400) {
|
|
http_response_code($httpCode);
|
|
// Intentar decodificar el cuerpo del error si Shalom lo envía en JSON
|
|
$errorBody = json_decode($response, true);
|
|
if (json_last_error() === JSON_ERROR_NONE && isset($errorBody['message'])) {
|
|
echo json_encode(['error' => "Error de la API de Shalom: " . $errorBody['message']]);
|
|
} else {
|
|
echo json_encode(['error' => "Error de la API de Shalom (código {$httpCode}).", 'details' => $response]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// 5. Devolver la respuesta exitosa al cliente
|
|
echo $response;
|