问题
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