]> _ Git - cubist_net.git/commitdiff
#7674
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 31 Jul 2025 13:36:55 +0000 (15:36 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 31 Jul 2025 13:36:55 +0000 (15:36 +0200)
src/Util.php

index d34c88eef28deda7bddbef89731d56d83cef8624..033cad8fd6a68c1302dc482af451dc20a7f10fbb 100644 (file)
@@ -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('/<meta\s+http-equiv=["\']refresh["\']\s+content=["\']\d+;\s*url=(["\']?)([^"\'>]*)/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;
+        }
+    }
+
+
+}