Update send_media_message.php

This commit is contained in:
Lizandro Guarnizo
2026-01-22 11:40:40 -05:00
parent 66dc55cc15
commit 9ee82c6c3f
+59 -5
View File
@@ -101,6 +101,37 @@ try {
error_log("send_media_message.php - Input decoded: " . json_encode($input));
smm_log("Input decoded: " . json_encode($input));
// Debug flag: permitir depuración remota si se pasa debug=true en query o en body
$debugMode = (isset($_GET['debug']) && $_GET['debug'] === 'true') || (!empty($input['debug']));
$debugInfo = [];
// Helper: hacer HEAD request para verificar accesibilidad de URL de media
function headCheckUrl($url) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 10
]);
$res = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$contentLength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$err = curl_error($ch);
if (version_compare(PHP_VERSION, '8.5.0', '<')) {
curl_close($ch);
}
return [
'http_code' => $httpCode,
'content_type' => $contentType,
'content_length' => $contentLength,
'error' => $err
];
}
// Validar campos requeridos
if (empty($input['recipient']) || empty($input['media_url']) || empty($input['media_type'])) {
@@ -172,7 +203,22 @@ try {
}
} else {
// URL pública, usar método normal con link
error_log("send_media_message.php - URL pública detectada, usando link directo");
error_log("send_media_message.php - URL pública detectada, usando link directo: " . $mediaUrl);
smm_log("Public URL: " . $mediaUrl);
// Chequear accesibilidad de la URL antes de pedir a WhatsApp que la descargue
try {
$head = headCheckUrl($mediaUrl);
smm_log("HEAD check: " . json_encode($head));
if ($debugMode) $debugInfo['media_head'] = $head;
if (isset($head['http_code']) && intval($head['http_code']) >= 400) {
smm_log("send_media_message.php - Warning: media URL returned HTTP " . $head['http_code']);
}
} catch (Exception $e) {
smm_log("send_media_message.php - HEAD check failed: " . $e->getMessage());
if ($debugMode) $debugInfo['media_head_error'] = $e->getMessage();
}
$response = sendMediaByLinkToWhatsApp($whatsapp, $recipient, $mediaUrl, $mediaType, $caption, $filename);
}
@@ -205,6 +251,7 @@ try {
if ($response && $sentMessageId) {
error_log("send_media_message.php - Respuesta de WhatsApp exitosa: " . json_encode($response));
smm_log("WhatsApp response (successful): " . json_encode($response));
if ($debugMode) $debugInfo['whatsapp_response'] = $response;
// Guardar en base de datos
$db = Database::getInstance();
@@ -242,12 +289,15 @@ try {
}
}
echo json_encode([
$out = [
'success' => true,
'message' => 'Mensaje multimedia enviado correctamente',
'media_type' => $mediaType,
'data' => $response
]);
];
if ($debugMode) $out['debug'] = $debugInfo;
echo json_encode($out);
} else {
error_log("send_media_message.php - Respuesta de WhatsApp sin message_id: " . json_encode($response));
@@ -267,11 +317,15 @@ try {
if (ob_get_length()) ob_clean();
http_response_code(500);
echo json_encode([
$errOut = [
'success' => false,
'error' => $e->getMessage(),
'file' => basename($e->getFile()),
'line' => $e->getLine()
], JSON_UNESCAPED_UNICODE);
];
if (isset($debugMode) && $debugMode) {
$errOut['debug'] = $debugInfo;
}
echo json_encode($errOut, JSON_UNESCAPED_UNICODE);
}
?>