]> _ Git - cubist_net.git/commitdiff
#7825 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 19 Nov 2025 15:07:19 +0000 (16:07 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 19 Nov 2025 15:07:19 +0000 (16:07 +0100)
composer.json
src/HTTP.php

index 54eb09405f0c6c4ffc67eabaaf9fda7729e45450..4995098f8a6fb6cea6f44d52c91096a1dca125dd 100644 (file)
@@ -28,7 +28,8 @@
         "ext-ftp": "*",
         "aws/aws-sdk-php": "^3.325",
         "google/cloud-storage": "*",
-        "ext-json": "*"
+        "ext-json": "*",
+        "ext-curl": "*"
     },
     "suggest": {
     }
index 4646c6d3180fb24988593847a3c24eee455c5136..d97234c94d9e485cb6dd45de2845bf1137804ed8 100644 (file)
@@ -7,46 +7,32 @@ class HTTP
     /**
      * @param string $url
      * @param string|null $flaresolverr
-     * @return int
+     * @return array
      */
-    public static function getResponseCode(string $url, string $flaresolverr = null): int
+    public static function getResponseCode(string $url, $timeout = 30, string $flaresolverr = null): array
     {
-        $headers = get_headers($url, 1);
-        if ($headers['Server'] === "cloudflare") {
-            $response = self::getHttpCodeCloudflare($url);
-        } else {
-            $response = self::getHttpCode($url);
+        if (null !== $flaresolverr) {
+            $headers = get_headers($url, 1);
+            if ($headers['Server'] === "cloudflare") {
+                return self::getHttpCodeCloudflare($url, $flaresolverr, $timeout);
+            }
         }
-        return (int)$response;
+        return self::getHttpCode($url, $timeout);
     }
 
-    public static function getHttpCodeCloudflare($url)
+    public static function getHttpCodeCloudflare($url, $apiUrl, $timeout = 30)
     {
-        $apiUrl = 'http://flaresolverr:8191/v1';
-
         $payload = json_encode([
             'cmd' => 'request.get',
             'url' => $url,
-            'maxTimeout' => 60000
+            'maxTimeout' => $timeout * 1000,
         ]);
 
-        /*$flaresolverrOptions = [
-            CURLOPT_POST => true,
-            CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
-            CURLOPT_POSTFIELDS => $payload,
-        ];*/
 
         $ch = curl_init($apiUrl);
-        self::$curlOpt = [
-            CURLOPT_RETURNTRANSFER => true,
-            CURLOPT_HEADER => true,
-            CURLOPT_NOBODY => true,
-            CURLOPT_TIMEOUT => self::$timeout,
-            CURLOPT_POST => true,
+        self::setCurlOpt($ch, $timeout, [CURLOPT_POST => true,
             CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
-            CURLOPT_POSTFIELDS => $payload,
-        ];
-        curl_setopt_array($ch, self::$curlOpt);
+            CURLOPT_POSTFIELDS => $payload]);
         $response = curl_exec($ch);
 
         if (preg_match('/(error|code) \b(301|302|308|404|401|403|405|500|502|503)\b/', $response, $matches)) {
@@ -60,15 +46,14 @@ class HTTP
         return ['httpcode' => $httpcode, 'finalurl' => $finalUrl, 'finalHttpCode' => $finalHttpCode];
     }
 
-    protected static function setCurlOpt($moreOptions = [])
+    protected static function setCurlOpt($ch, $timeout = 30, $moreOptions = [])
     {
-        self::$curlOpt = [
-            CURLOPT_RETURNTRANSFER => true,
-            CURLOPT_HEADER => true,
-            CURLOPT_NOBODY => true,
-            CURLOPT_TIMEOUT => self::$timeout,
-            ...$moreOptions
-        ];
+        curl_setopt_array($ch, array_merge([
+                CURLOPT_RETURNTRANSFER => true,
+                CURLOPT_HEADER => true,
+                CURLOPT_NOBODY => true,
+                CURLOPT_TIMEOUT => $timeout], $moreOptions)
+        );
     }