我有一个包含 URL 列表的 txt 文件。我想要该列表中所有 URL 的重定向位置。这是我的代码:
$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>";
}
这段代码的问题是它只显示列表中最后一个 URL 的重定向位置。有谁知道这个问题的解决方案?