message}\n", 'r'); } } else { // Process the XML object return $data; } } } $data = self::fetch_data_with_retries($url); if ($data === false && $debug) { Notice::note("Error: Failed to load XML from $url", 'r', 1); foreach (libxml_get_errors() as $error) { Notice::note("XML Error: {$error->message}\n", 'r'); } } if ($save) { if (! file_exists($filename)) { Notice::note("Error: File $filename does not exist.", 'r', 1); } elseif (! is_readable($filename)) { Notice::note("Error: File $filename is not readable.", 'r', 1); } elseif (! is_writable($filename)) { Notice::note("Error: File $filename is not writable.", 'r', 1); } else { $data->asXml($filename); Notice::note('SAVED AND EXIT', 'b', 1); } } return $data; } public static function fetch_data_with_retries($url, $max_retries = 3) { $attempt = 0; $timeout = 10; // seconds $connect_timeout = 5; // seconds while ($attempt < $max_retries) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // Total timeout curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout); // Connection timeout $response = curl_exec($ch); if ($response === false) { $attempt++; echo "Attempt $attempt failed. Retrying...\n"; sleep(2); // Optional: Sleep between retries } else { curl_close($ch); if (mb_substr($response, 0, 1) != '<') { Log::debug('INVALID XML '.$url.'----'.$response); } else { return new SimpleXMLElement($response); } } } echo "All attempts failed.\n"; curl_close($ch); return null; // Return null if all retries fail } }