I need Help with my php array

What im trying to do is grab multiple links from file and for each get all redirects.
Some links have more than 1 redirect (3 or 4)
I want them all , the code that I found works perfectly but it only stops at the second redirect .

this is my code


<?php
function get_redirect_url($url){
    $redirect_url = null; 

    $url_parts = @parse_url($url);
    if (!$url_parts) return false;
    if (!isset($url_parts['host'])) return false; //can't process relative URLs
    if (!isset($url_parts['path'])) $url_parts['path'] = '/';

    $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
    if (!$sock) return false;

    $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n"; 
    $request .= 'Host: ' . $url_parts['host'] . "\r\n"; 
    $request .= "Connection: Close\r\n\r\n"; 
    fwrite($sock, $request);
    $response = '';
    while(!feof($sock)) $response .= fread($sock, 8192);
    fclose($sock);

    if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
        if ( substr($matches[1], 0, 1) == "/" )
            return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
        else
            return trim($matches[1]);

    } else {
        return false;
    }

}

function get_all_redirects($url){
    $redirects = array();
    while ($newurl = get_redirect_url($url)){
        if (in_array($newurl, $redirects)){
            break;
        }
        $redirects[] = $newurl;
        $url = $newurl;
    }
    return $redirects;
}

?>

<?php
$urls = file_get_contents("https://traddiwdice.com/links.txt");

$url_list = explode("\n", $urls);

$file_content = '';
foreach ($url_list as $url){
    $rez = get_all_redirects($url);
    $file_content .= "$url,";
    foreach ($rez as $v){
        $file_content .= "$v,";
    }
    $file_content = substr($file_content,0, -1);
    $file_content .= "\n";
    
}

echo $file_content ;
?>

and those are the links

https://stvkr.com/v2/click-30Y7Y-8X5JXO-WDM1a-f4390b89?tl=1&no_js=1
https://stvkr.com/v2/click-35g74-8X5JXO-7mxNQ-715caedb?tl=1&no_js=1
https://stvkr.com/v2/click-b9Nda-8X5JXO-RyjjG-9c2b5dbe?tl=1&no_js=1
https://stvkr.com/v2/click-3m1Wo-8X5JXO-eV1D6-a7c442dc?tl=1&no_js=1
https://stvkr.com/v2/click-3m1yq-8X5JXO-07W8y-8bb08227?tl=1&no_js=1
https://stvkr.com/v2/click-Bl7qR-8X5JXO-aDJvj-d7a2b7f2?tl=1&no_js=1
https://stvkr.com/v2/click-AwOgz-8X5JXO-PDzL0-bec652ac?tl=1&no_js=1
https://stvkr.com/v2/click-Bl75Y-8X5JXO-Y2GVM-606b3595?tl=1&no_js=1
https://stvkr.com/v2/click-b2mKY-8X5JXO-8Vzor-a939a4e2?tl=1&no_js=1
https://stvkr.com/v2/click-bnlgR-8X5JXO-jP7v9-97b0011f?tl=1&no_js=1
https://stvkr.com/v2/click-AjlVQ-8X5JXO-PDGOk-a24b98fe?tl=1&no_js=1
https://stvkr.com/v2/click-AjlZ0-8X5JXO-nY5zR-af5c5a80?tl=1&no_js=1
https://stvkr.com/v2/click-B7oOQ-8X5JXO-gx1Ww-e068385e?tl=1&no_js=1
https://stvkr.com/v2/click-35oWk-8X5JXO-lQkXK-b4d0dfec?tl=1&no_js=1
https://stvkr.com/v2/click-AMy64-8X5JXO-gx1GN-7b68129f?tl=1&no_js=1
https://stvkr.com/v2/click-3WE6g-8X5JXO-6GKgN-160b6c0a?tl=1&no_js=1
https://stvkr.com/v2/click-BNgNo-8X5JXO-Ryazl-550b5997?tl=1&no_js=1
https://stvkr.com/v2/click-Aj4LQ-8X5JXO-aDkgp-93388dd3?tl=1&no_js=1
https://stvkr.com/v2/click-ALrZN-8X5JXO-J05yK-52bff0bc?tl=1&no_js=1
https://ytthn.com/v2/click-bk1q9-8X5JXO-PDDxk-f8147b98?tl=1&no_js=1
https://stvkr.com/v2/click-BEk79-8X5JXO-RyMJQ-c63d2924?tl=1&no_js=1
https://stvkr.com/v2/click-BPpD2-8X5JXO-bzlXY-f9c09286?tl=1&no_js=1
https://stvkr.com/v2/click-b980K-8X5JXO-3WzDJ-4ac90e16?tl=1&no_js=1
https://stvkr.com/v2/click-3oapL-8X5JXO-LqamW-7ef4e658?tl=1&no_js=1
https://stvkr.com/v2/click-AG5wK-8X5JXO-3YYEk-9501fe68?tl=1&no_js=1
https://stvkr.com/v2/click-3oE7r-8X5JXO-B4qgM-ce79fe38?tl=1&no_js=1
https://stvkr.com/v2/click-AvvQA-8X5JXO-3yzMN-150aaf44?tl=1&no_js=1

can someone help please

and if possible not echo the original link
Only the redirects

Sponsor our Newsletter | Privacy Policy | Terms of Service