109 lines
3.5 KiB
PHP
109 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Utilities\Notice;
|
|
use SimpleXMLElement;
|
|
|
|
class Xml
|
|
{
|
|
public static $file;
|
|
|
|
public static function load($url)
|
|
{
|
|
$debug = Nuh::$options['d'] ?? 0;
|
|
$save = Nuh::$options['s'] ?? 0;
|
|
$read = Nuh::$options['r'] ?? 0;
|
|
|
|
$filename = app_path().'/Console/Commands/TestFiles/'.strtolower(str_replace(' ', '_', Nuh::$options['name'])).'.xml';
|
|
|
|
if ($debug) {
|
|
Notice::note("Loading URL\n".$url);
|
|
}
|
|
|
|
if ($save || $read) {
|
|
Notice::note("FILE NAME\n".$filename);
|
|
}
|
|
|
|
if ($read) {
|
|
if ($debug) {
|
|
Notice::note('READ LOCAL', 'r');
|
|
}
|
|
|
|
// return simplexml_load_file(self::$file, null, LIBXML_NOCDATA);
|
|
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);
|
|
} else {
|
|
$data = simplexml_load_file($filename, 'SimpleXMLElement', LIBXML_NOCDATA);
|
|
if ($data === false) {
|
|
Notice::note("Error: Failed to load XML from $filename", 'r', 1);
|
|
foreach (libxml_get_errors() as $error) {
|
|
Notice::note("XML Error: {$error->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);
|
|
|
|
return new SimpleXMLElement($response);
|
|
}
|
|
}
|
|
echo "All attempts failed.\n";
|
|
curl_close($ch);
|
|
|
|
return null; // Return null if all retries fail
|
|
}
|
|
}
|