How can I determine if a URL redirects in PHP?

前端 未结 3 966
我寻月下人不归
我寻月下人不归 2020-12-08 23:52

I saw someone ask a question about detecting if a URL redirects from groovy and perl but couldn\'t find anything on PHP.

Anyone know of somewhere I could find that c

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 23:59

    Actually, I found this works best:

        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;
        }
    

提交回复
热议问题