up
This commit is contained in:
@@ -20,6 +20,7 @@ class WhatsAppService
|
||||
private $phoneNumberId;
|
||||
private $apiUrl;
|
||||
private $db;
|
||||
private $rateLimitMonitor;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -30,6 +31,25 @@ class WhatsAppService
|
||||
$this->phoneNumberId = $config['phone_number_id']; // Usar phone_number_id de BD
|
||||
$this->apiUrl = $config['api_url'] ?: 'https://graph.facebook.com/v22.0/'; // Usar api_url de BD
|
||||
$this->db = Database::getInstance();
|
||||
|
||||
// Inicializar monitor de rate limit
|
||||
try {
|
||||
// Cargar autoloader si no está cargado
|
||||
if (!class_exists('Predis\Client')) {
|
||||
$autoloadPath = __DIR__ . '/../vendor/autoload.php';
|
||||
if (file_exists($autoloadPath)) {
|
||||
require_once $autoloadPath;
|
||||
}
|
||||
}
|
||||
|
||||
if (file_exists(__DIR__ . '/RateLimitMonitor.php')) {
|
||||
require_once __DIR__ . '/RateLimitMonitor.php';
|
||||
$this->rateLimitMonitor = new RateLimitMonitor();
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("WhatsAppService: No se pudo inicializar RateLimitMonitor - " . $e->getMessage());
|
||||
$this->rateLimitMonitor = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -561,7 +581,8 @@ class WhatsAppService
|
||||
CURLOPT_SSL_VERIFYPEER => true,
|
||||
CURLOPT_SSL_VERIFYHOST => 2,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_MAXREDIRS => 3
|
||||
CURLOPT_MAXREDIRS => 3,
|
||||
CURLOPT_HEADER => true, // Capturar headers de respuesta
|
||||
]);
|
||||
|
||||
if ($method === 'POST' && $data) {
|
||||
@@ -569,13 +590,27 @@ class WhatsAppService
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
}
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$fullResponse = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||||
$error = curl_error($ch);
|
||||
|
||||
// Separar headers y body
|
||||
$headerText = substr($fullResponse, 0, $headerSize);
|
||||
$response = substr($fullResponse, $headerSize);
|
||||
|
||||
// curl_close is deprecated in PHP 8.5+ (has no effect). Avoid calling it on newer versions.
|
||||
if (version_compare(PHP_VERSION, '8.5.0', '<')) {
|
||||
curl_close($ch);
|
||||
}
|
||||
|
||||
// Capturar rate limit info de los headers
|
||||
if ($this->rateLimitMonitor && $headerText) {
|
||||
$endpoint = parse_url($url, PHP_URL_PATH);
|
||||
// Parsear headers string a array
|
||||
$headersArray = array_filter(explode("\r\n", $headerText));
|
||||
$this->rateLimitMonitor->captureFromHeaders($headersArray, $endpoint);
|
||||
}
|
||||
|
||||
if ($error) {
|
||||
error_log("cURL Error: " . $error);
|
||||
|
||||
Reference in New Issue
Block a user