From 95d248e2b6b92d646a0f55757e30f8a9fb1685db Mon Sep 17 00:00:00 2001 From: Robin Wieske Date: Fri, 13 Dec 2024 12:45:35 +0100 Subject: [PATCH] Uses curl to fetch k6 binary to solve github redirect issues Makes use of `CURLOPT_FOLLOWLOCATION` to allow a redirect on github. --- src/K6.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/K6.php b/src/K6.php index eafd4db..87eff24 100644 --- a/src/K6.php +++ b/src/K6.php @@ -80,10 +80,20 @@ public static function download(): void $url = sprintf(self::K6_URL, self::K6_VERSION, self::K6_VERSION, $os, $arch, $extension); $fileName = basename($url); - if (false === ($binary = file_get_contents($url))) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + + $binary = curl_exec($ch); + + if (false === $binary || 200 !== curl_getinfo($ch, CURLINFO_HTTP_CODE)) { + curl_close($ch); throw new RuntimeException('Unable to download k6 binary.'); } + curl_close($ch); + if (file_put_contents(self::BIN_DIR.$fileName, $binary) === false) { throw new RuntimeException('Unable to save k6 binary.'); }