feat: perfil WhatsApp (foto, about, datos) y reset+sync de plantillas
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/**
|
||||
* API - Subir foto de perfil de WhatsApp Business
|
||||
* 1. Sube la imagen como media a la API de WhatsApp
|
||||
* 2. Usa el media_handle obtenido para actualizar la foto de perfil
|
||||
*/
|
||||
|
||||
require_once '../config/config.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
requireAuthentication();
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
echo json_encode(['success' => false, 'error' => 'Método no permitido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (empty($_FILES['photo']) || $_FILES['photo']['error'] !== UPLOAD_ERR_OK) {
|
||||
$code = $_FILES['photo']['error'] ?? -1;
|
||||
echo json_encode(['success' => false, 'error' => "No se recibió archivo válido (código {$code})"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$file = $_FILES['photo'];
|
||||
|
||||
// Validar tipo MIME
|
||||
$allowedMimes = ['image/jpeg', 'image/png'];
|
||||
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
||||
$mime = $finfo->file($file['tmp_name']);
|
||||
if (!in_array($mime, $allowedMimes, true)) {
|
||||
echo json_encode(['success' => false, 'error' => 'Solo se permiten imágenes JPG o PNG']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validar tamaño (máx 5 MB)
|
||||
if ($file['size'] > 5 * 1024 * 1024) {
|
||||
echo json_encode(['success' => false, 'error' => 'La imagen no puede superar 5 MB']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$config = getWhatsAppConfigFromDB();
|
||||
$token = $config['token'] ?? '';
|
||||
$phoneId = $config['phone_number_id'] ?? '';
|
||||
$apiUrl = rtrim($config['api_url'] ?: 'https://graph.facebook.com/v22.0/', '/');
|
||||
|
||||
if (empty($token) || empty($phoneId)) {
|
||||
echo json_encode(['success' => false, 'error' => 'Token o Phone Number ID no configurado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// ── Paso 1: subir la imagen como media ───────────────────────────────────
|
||||
$uploadUrl = "{$apiUrl}/{$phoneId}/media";
|
||||
|
||||
$cfile = new CURLFile($file['tmp_name'], $mime, basename($file['name']));
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $uploadUrl,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'file' => $cfile,
|
||||
'type' => $mime,
|
||||
],
|
||||
CURLOPT_HTTPHEADER => ["Authorization: Bearer {$token}"],
|
||||
CURLOPT_TIMEOUT => 30,
|
||||
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
||||
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
|
||||
]);
|
||||
|
||||
$uploadResponse = curl_exec($ch);
|
||||
$uploadCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$uploadError = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($uploadError) {
|
||||
throw new Exception("Error al subir imagen: {$uploadError}");
|
||||
}
|
||||
|
||||
$uploadData = json_decode($uploadResponse, true);
|
||||
if ($uploadCode !== 200 || empty($uploadData['id'])) {
|
||||
$msg = $uploadData['error']['message'] ?? 'Error desconocido al subir';
|
||||
throw new Exception("Upload error ({$uploadCode}): {$msg}");
|
||||
}
|
||||
|
||||
$mediaHandle = $uploadData['id'];
|
||||
|
||||
// ── Paso 2: actualizar la foto de perfil con el handle ───────────────────
|
||||
$profileUrl = "{$apiUrl}/{$phoneId}/whatsapp_business_profile";
|
||||
|
||||
$payload = [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'profile_picture_handle' => $mediaHandle,
|
||||
];
|
||||
|
||||
$ch2 = curl_init();
|
||||
curl_setopt_array($ch2, [
|
||||
CURLOPT_URL => $profileUrl,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => json_encode($payload),
|
||||
CURLOPT_HTTPHEADER => [
|
||||
"Authorization: Bearer {$token}",
|
||||
"Content-Type: application/json",
|
||||
],
|
||||
CURLOPT_TIMEOUT => 20,
|
||||
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
||||
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
|
||||
]);
|
||||
|
||||
$profileResponse = curl_exec($ch2);
|
||||
$profileCode = curl_getinfo($ch2, CURLINFO_HTTP_CODE);
|
||||
$profileError = curl_error($ch2);
|
||||
curl_close($ch2);
|
||||
|
||||
if ($profileError) {
|
||||
throw new Exception("Error al actualizar foto: {$profileError}");
|
||||
}
|
||||
|
||||
$profileData = json_decode($profileResponse, true);
|
||||
if ($profileCode !== 200) {
|
||||
$msg = $profileData['error']['message'] ?? 'Error desconocido al actualizar foto';
|
||||
throw new Exception("Profile photo error ({$profileCode}): {$msg}");
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true, 'message' => 'Foto de perfil actualizada correctamente']);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log('[upload_whatsapp_profile_photo] ' . $e->getMessage());
|
||||
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
||||
}
|
||||
Reference in New Issue
Block a user