Get redirect location of URL list

大憨熊 提交于 2019-12-12 02:31:49

问题


I have a txt file with a list of URLs. I want the redirect locations of all the URLs in that list. Here is my code:

$url_list = "ggurl.txt";


$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

foreach (file($url_list) as $url) {

    curl_setopt($ch, CURLOPT_URL, $url);
    $out = curl_exec($ch);

    // line endings is the wonkiest piece of this whole thing
    $out = str_replace("\r", "", $out);

    // only look at the headers
    $headers_end = strpos($out, "\n\n");
    if( $headers_end !== false ) { 
        $out = substr($out, 0, $headers_end);
    }   

    $headers = explode("\n", $out);
    foreach($headers as $header) {
        if( substr($header, 0, 10) == "Location: " ) { 
            $target = substr($header, 10);

            echo "[$url] redirects to [$target]<br>";
            $url = $target;
            continue 2;
        }
    }

$url = rtrim($url);

echo $url . "<br>";

}

The problem with this code is that it only shows the redirect location of the last URL in the list. Does anyone know a solution to this problem?

来源:https://stackoverflow.com/questions/14315161/get-redirect-location-of-url-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!