通过PHP的get_headers()函数来获取跳转后的网址
- /*
- 获取多次跳转后真实的url
- @param str $url 查询
- $return str 定向后的url的真实url
- */
- function getrealurl($url){
- $header = @get_headers($url,1); //默认第二个参数0,可选1,返回关联数组
- if(!$header){
- exit('无法打开此网站'.$url);
- }
- //var_dump($header);
- if (strpos($header[0],'301') || strpos($header[0],'302')) {
- if(is_array($header['Location'])) {
- return $header['Location'][count($header['Location'])-1];
- }else{
- return $header['Location'];
- }
- }else {
- return $url;
- }
- }
- $url = 'http://t.cn/EX1RqUk';
- $url = getrealurl($url);
- echo '真实的url为:'.$url;