我看到有人问一个关于检测URL是否从groovy和perl重定向但在PHP上找不到任何内容的问题.
任何人都知道某处我可以找到这样做的代码?
$ch = curl_init('http://www.yahoo.com/'); curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (($code == 301) || ($code == 302)) { //This was a redirect }
实际上,我发现这个效果最好:
function GetURL($URL) { $ch = curl_init($URL); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); curl_close($ch); return $code; }