选项1 – 卷曲
使用CURL并将CURLOPT_FOLLOWLOCATION选项设置为true:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http//example.com");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(curl_exec($ch) === FALSE) {
echo "Error: " . curl_error($ch);
} else {
echo curl_exec($ch);
}
curl_close($ch);
?>
选项2 – file_get_contents
根据PHP Documentation file_get_contents(),默认情况下将遵循20个重定向.因此,您可以使用该功能.失败时,file_get_contents()将返回FALSE,否则它将返回整个文件.
$string = file_get_contents("http://www.example.com");
if($string === FALSE) {
echo "Could not read the file.";
} else {
echo $string;
}
?>