This commit is contained in:
Lizandro Guarnizo
2026-02-21 09:29:27 -05:00
parent 07333be7ce
commit 1bd7588e86
2 changed files with 47 additions and 20 deletions
+44 -19
View File
@@ -22,12 +22,14 @@ class MediaService {
if (!is_dir($this->uploadsDir)) mkdir($this->uploadsDir, 0755, true); if (!is_dir($this->uploadsDir)) mkdir($this->uploadsDir, 0755, true);
} }
public function fetchAndStoreFromGraph($mediaId, $subdir = '') { /**
// Obtener metadata (url) via Graph API * Hacer petición GET al Graph API con fallback a wget si curl falla
$endpoint = "{$this->apiUrl}/{$mediaId}"; */
private function graphApiRequest($url) {
// Intentar con curl primero
$ch = curl_init(); $ch = curl_init();
curl_setopt_array($ch, [ curl_setopt_array($ch, [
CURLOPT_URL => $endpoint, CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $this->token], CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $this->token],
CURLOPT_TIMEOUT => 15, CURLOPT_TIMEOUT => 15,
@@ -39,8 +41,36 @@ class MediaService {
$err = curl_error($ch); $err = curl_error($ch);
curl_close($ch); curl_close($ch);
if ($err || $http >= 400) { if (!$err && $http >= 200 && $http < 400 && $resp) {
throw new Exception('Graph API fetch failed: ' . $err); return $resp;
}
// Fallback a wget para la petición al Graph API
error_log("[MediaService::graphApiRequest] curl failed (http={$http} err={$err}), trying wget...");
$tmpFile = tempnam(sys_get_temp_dir(), 'graph_');
$cmd = 'wget -q --prefer-family=IPv4 -O ' . escapeshellarg($tmpFile)
. ' --header=' . escapeshellarg('Authorization: Bearer ' . $this->token)
. ' ' . escapeshellarg($url) . ' 2>&1';
exec($cmd, $output, $exitCode);
if ($exitCode === 0 && file_exists($tmpFile) && filesize($tmpFile) > 0) {
$content = file_get_contents($tmpFile);
unlink($tmpFile);
error_log("[MediaService::graphApiRequest] wget OK, " . strlen($content) . " bytes");
return $content;
}
error_log("[MediaService::graphApiRequest] wget also failed exit={$exitCode}");
if (file_exists($tmpFile)) unlink($tmpFile);
return false;
}
public function fetchAndStoreFromGraph($mediaId, $subdir = '') {
// Obtener metadata (url) via Graph API
$endpoint = "{$this->apiUrl}/{$mediaId}";
$resp = $this->graphApiRequest($endpoint);
if ($resp === false) {
throw new Exception('Graph API fetch failed for media ' . $mediaId);
} }
$decoded = json_decode($resp, true); $decoded = json_decode($resp, true);
$mediaUrl = $decoded['url'] ?? ($decoded['data'][0]['url'] ?? null); $mediaUrl = $decoded['url'] ?? ($decoded['data'][0]['url'] ?? null);
@@ -50,19 +80,14 @@ class MediaService {
$content = $this->downloadWithWget($mediaUrl, !empty($this->token) ? ['Authorization: Bearer ' . $this->token] : []); $content = $this->downloadWithWget($mediaUrl, !empty($this->token) ? ['Authorization: Bearer ' . $this->token] : []);
if ($content === false) { if ($content === false) {
// Obtener URL fresca del Graph API (la anterior puede haber expirado tras el intento fallido) // Obtener URL fresca del Graph API (la anterior puede haber expirado tras el intento fallido)
error_log("[MediaService] wget failed, trying curl with fresh URL..."); error_log("[MediaService] wget download failed, getting fresh URL...");
$ch2 = curl_init($endpoint); $resp2 = $this->graphApiRequest($endpoint);
curl_setopt_array($ch2, [ if ($resp2) {
CURLOPT_RETURNTRANSFER => true, $decoded2 = json_decode($resp2, true);
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $this->token], $freshUrl = $decoded2['url'] ?? $mediaUrl;
CURLOPT_TIMEOUT => 15, } else {
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, $freshUrl = $mediaUrl;
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, }
]);
$resp2 = curl_exec($ch2);
curl_close($ch2);
$decoded2 = json_decode($resp2, true);
$freshUrl = $decoded2['url'] ?? $mediaUrl;
$content = $this->downloadUrl($freshUrl); $content = $this->downloadUrl($freshUrl);
} }
+3 -1
View File
@@ -26,7 +26,9 @@ $processed = 0;
$success = 0; $success = 0;
$failed = 0; $failed = 0;
foreach ($rows as $r) { foreach ($rows as $idx => $r) {
// Pausa entre items para evitar saturar conexiones a Facebook
if ($idx > 0) sleep(2);
$processed++; $processed++;
$attempts = intval($r['attempts'] ?? 0) + 1; $attempts = intval($r['attempts'] ?? 0) + 1;