Php retrieves a simple 301 jump URL instance. Copy the code as follows: *** get_redirect_url () * GetstheaddressthattheprovidedURLredirectsto, * orFALSEiftheresnoredirect. ** @ paramstring $ url * @ returnstrin
The code is as follows:
/**
* Get_redirect_url ()
* Gets the address that the provided URL redirects,
* Or FALSE if there's 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']: 80), $ 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 ";
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 uses socket to get the 301 jump address, which can extract the url during the jump process
The http://www.bkjia.com/PHPjc/621688.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/621688.htmlTechArticle code is as follows:/*** get_redirect_url () * Gets the address that the provided URL redirects to, * or FALSE if there's no redirect. ** @ param string $ url * @ return strin...