我发现这个调用的函数get_all_redirects
可以完成这项工作:
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;
}
你可以这样使用它:
$urls = file_get_contents("urls.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";
}
file_put_contents("output.csv", $file_content);
urls.txt
是一个包含您的网址的文本文件(每行一个网址):
http://url1.com
http://url2.com
http://url3.com
...
http://urlN.com