/** * Get_redirect_url () * Gets The address of the provided URL redirects to, * or FALSE if there ' no redirect. * * @param string $url * @return string */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.1\r\n"; $request. = ' Host: '. $url _parts[' host ']. "\ r \ n"; $request. = "connection:close\r\n\r\n"; 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; }}/** * Get_all_redirects () * follows and collects all redirects, in order, for the given URL. * * @param string $url * @return array */function get_all_redirects ($url) {$redirects = array (); while ($newurl = Get_redirect_url ($url)) {if (In_array ($newurl, $redirects)) {break; } $redirects [] = $newurl; $url = $newurl; } return $redirects;}PHP implementation with the socket to get 301 jump address, you can extract the URL during the jump
http://www.bkjia.com/PHPjc/621629.html www.bkjia.com true http://www.bkjia.com/PHPjc/621629.html techarticle /** * Get_redirect_url () * Gets The address of the provided URL redirects to, * or FALSE if there ' s no redirect. * @param string $url * @return string */function get_redirect_u ...