up
This commit is contained in:
+44
-19
@@ -22,12 +22,14 @@ class MediaService {
|
||||
if (!is_dir($this->uploadsDir)) mkdir($this->uploadsDir, 0755, true);
|
||||
}
|
||||
|
||||
public function fetchAndStoreFromGraph($mediaId, $subdir = '') {
|
||||
// Obtener metadata (url) via Graph API
|
||||
$endpoint = "{$this->apiUrl}/{$mediaId}";
|
||||
/**
|
||||
* Hacer petición GET al Graph API con fallback a wget si curl falla
|
||||
*/
|
||||
private function graphApiRequest($url) {
|
||||
// Intentar con curl primero
|
||||
$ch = curl_init();
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $endpoint,
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $this->token],
|
||||
CURLOPT_TIMEOUT => 15,
|
||||
@@ -39,8 +41,36 @@ class MediaService {
|
||||
$err = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($err || $http >= 400) {
|
||||
throw new Exception('Graph API fetch failed: ' . $err);
|
||||
if (!$err && $http >= 200 && $http < 400 && $resp) {
|
||||
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);
|
||||
$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] : []);
|
||||
if ($content === false) {
|
||||
// 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...");
|
||||
$ch2 = curl_init($endpoint);
|
||||
curl_setopt_array($ch2, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $this->token],
|
||||
CURLOPT_TIMEOUT => 15,
|
||||
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
]);
|
||||
$resp2 = curl_exec($ch2);
|
||||
curl_close($ch2);
|
||||
$decoded2 = json_decode($resp2, true);
|
||||
$freshUrl = $decoded2['url'] ?? $mediaUrl;
|
||||
error_log("[MediaService] wget download failed, getting fresh URL...");
|
||||
$resp2 = $this->graphApiRequest($endpoint);
|
||||
if ($resp2) {
|
||||
$decoded2 = json_decode($resp2, true);
|
||||
$freshUrl = $decoded2['url'] ?? $mediaUrl;
|
||||
} else {
|
||||
$freshUrl = $mediaUrl;
|
||||
}
|
||||
|
||||
$content = $this->downloadUrl($freshUrl);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,9 @@ $processed = 0;
|
||||
$success = 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++;
|
||||
$attempts = intval($r['attempts'] ?? 0) + 1;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user