我有一个包含网址列表的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的重定向位置。有谁知道这个问题的解决方案?