From: Vincent Vanwaelscappel Date: Thu, 31 Jul 2025 13:36:55 +0000 (+0200) Subject: #7674 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=3513379e4610eee666255b112b38b50f415e1770;p=cubist_net.git #7674 --- diff --git a/src/Util.php b/src/Util.php index d34c88e..033cad8 100644 --- a/src/Util.php +++ b/src/Util.php @@ -2,6 +2,10 @@ namespace Cubist\Net; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\RequestException; +use Illuminate\Support\Facades\Log; + class Util { public static function resolve($hostOrCIDR) @@ -47,4 +51,28 @@ class Util return false; } -} \ No newline at end of file + + public static function getFinalUrl($url) + { + $client = new Client(['allow_redirects' => ['track_redirects' => true]]); + + try { + $response = $client->request('GET', $url); + $redirectHistory = $response->getHeader('X-Guzzle-Redirect-History'); + + // Check if the response body contains a meta-refresh tag + $body = (string)$response->getBody(); + if (preg_match('/]*)/i', $body, $matches)) { + $metaRefreshUrl = $matches[2]; + return self::getFinalUrl($metaRefreshUrl); + } + + return end($redirectHistory) ?: $url; + } catch (RequestException $e) { + Log::error("Request failed: " . $e->getMessage()); + return null; + } + } + + +}