Now there are a lot of short URL applications, click on the jump to shorten the previous URL, then today we will look at, PHP how to get a short URL to jump before the URL, in fact, is also very simple, is the PHP get_headers function,
Get response Headers, then slowly analysis on the line.
Give a concrete implementation method:
| The code is as follows |
Copy Code |
$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; } |
Example Two
| The code is as follows |
Copy Code |
// echo get_redirect_url (' http://www.111cN.nEt '); The output is: Http://code.google.com/android/ function Get_redirect_url ($url) { $redirect _url = null; $url _parts = @parse_url ($url); if (! $url _parts) return false; if (!isset ($url _parts[' host ')) return false; Can ' t process relative URLs if (!isset ($url _parts[' path ')) $url _parts[' path '] = '/'; $sock = Fsockopen ($url _parts[' host '], (isset ($url _parts[' Port ')? (int) $url _parts[' Port '): $errno, $errstr, 30); if (! $sock) return false; $request = "HEAD". $url _parts[' path ']. (Isset ($url _parts[' query ')? '?'. $url _parts[' query ']: '). "Http/1.1rn"; $request. = ' Host: '. $url _parts[' host ']. "RN"; $request. = "Connection:closernrn"; Fwrite ($sock, $request); $response = "; while (!feof ($sock)) $response. = Fread ($sock, 8192); Fclose ($sock); if (Preg_match ('/^location: (. +?) $/m ', $response, $matches)) { if (substr ($matches [1], 0, 1) = = = "/") return $url _parts[' scheme '). "://" . $url _parts[' host ']. Trim ($matches [1]); Else return Trim ($matches [1]); } else { return false; } } |
http://www.bkjia.com/PHPjc/631515.html www.bkjia.com true http://www.bkjia.com/PHPjc/631515.html techarticle Now there are a lot of short URL applications, click on the jump to shorten the previous URL, then today we will look at, PHP how to get a short URL to jump before the URL, in fact, is also very simple, ...