取得Response Headers,之后慢慢分析就行了。
给出具体实现方法:
代码如下 | 复制代码 |
$header = get_headers($url, 1); 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; } |
实例二
代码如下 | 复制代码 |
<?php // echo get_redirect_url('http://www.111cN.nEt'); //输出结果为:http://code.google.com/android/ function get_redirect_url($url){ $redirect_url = null; $url_parts = @parse_url($url); $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30); $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1rn"; if (preg_match('/^Location: (.+?)$/m', $response, $matches)){ } else { } |