Update MediaService.php
This commit is contained in:
@@ -127,20 +127,53 @@ class MediaService {
|
||||
CURLOPT_TIMEOUT => 180,
|
||||
CURLOPT_HTTPHEADER => $headers,
|
||||
CURLOPT_BUFFERSIZE => 65536,
|
||||
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, // Forzar IPv4 (IPv6 no funciona en este host)
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, // Forzar HTTP/1.1 (HTTP/2 causa reset en lookaside.fbsbx.com)
|
||||
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
]);
|
||||
$content = curl_exec($ch);
|
||||
$err = curl_error($ch);
|
||||
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$errno = curl_errno($ch);
|
||||
curl_close($ch);
|
||||
|
||||
// Log para diagnóstico
|
||||
if ($err || $http >= 400) {
|
||||
error_log("[MediaService::downloadUrl] FAIL url=" . substr($url, 0, 120) . " http={$http} err={$err}");
|
||||
return false;
|
||||
// Si curl funcionó correctamente, retornar contenido
|
||||
if (!$err && $http >= 200 && $http < 400 && $content !== false && strlen($content) > 0) {
|
||||
return $content;
|
||||
}
|
||||
return $content;
|
||||
|
||||
// Fallback a wget cuando curl falla (OpenSSL incompatibilidad con Facebook CDN)
|
||||
error_log("[MediaService::downloadUrl] curl failed (http={$http} errno={$errno} err={$err}), trying wget fallback...");
|
||||
$content = $this->downloadWithWget($url, $headers);
|
||||
if ($content !== false) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
error_log("[MediaService::downloadUrl] FAIL url=" . substr($url, 0, 120) . " http={$http} err={$err} wget=also_failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fallback: descargar con wget (usa GnuTLS en vez de OpenSSL, más compatible con Facebook CDN)
|
||||
*/
|
||||
private function downloadWithWget($url, $headers = []) {
|
||||
$tmpFile = tempnam(sys_get_temp_dir(), 'wget_');
|
||||
$headerArgs = '';
|
||||
foreach ($headers as $h) {
|
||||
$headerArgs .= ' --header=' . escapeshellarg($h);
|
||||
}
|
||||
$cmd = 'wget -4 -q -O ' . escapeshellarg($tmpFile) . $headerArgs . ' ' . 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::downloadWithWget] OK, " . strlen($content) . " bytes");
|
||||
return $content;
|
||||
}
|
||||
|
||||
error_log("[MediaService::downloadWithWget] FAIL exit={$exitCode} output=" . implode(' ', $output));
|
||||
if (file_exists($tmpFile)) unlink($tmpFile);
|
||||
return false;
|
||||
}
|
||||
|
||||
public function extensionFromMime($mime) {
|
||||
|
||||
Reference in New Issue
Block a user