Parse php to get the redirected address (code ). Copy the code as follows: Get the redirected address classRedirectUrl {address var $ url; initialize the address functionRedirectUrl ($ url) {$ this-url $ url;} *** get_redirect_url ()
The code is as follows:
// Get the redirected address
Class RedirectUrl {
// Address
Var $ url;
// Initialize the address
Function RedirectUrl ($ url ){
$ This-> url = $ url;
}
/**
* Get_redirect_url ()
* Get the redirected address
*
* @ Param string $ url
* @ Return string
*/
Private 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 )){
Return trim ($ matches [1]);
} Else {
Return false;
}
}
/**
* Get_all_redirects ()
* Retrieve all redirection addresses
*
* @ Param string $ url
* @ Return array
*/
Private function get_all_redirects ($ url ){
$ Redirects = array ();
While ($ newurl = $ this-> get_redirect_url ($ url )){
If (in_array ($ newurl, $ redirects )){
Break;
}
$ Redirects [] = $ newurl;
$ Url = $ newurl;
}
Return $ redirects;
}
/**
* Get_final_url ()
* Obtain the actual address
*
* @ Param string $ url
* @ Return string
*/
Function get_final_url (){
$ Redirects = $ this-> get_all_redirects ($ this-> url );
If (count ($ redirects)> 0 ){
Return array_pop ($ redirects );
} Else {
Return $ this-> url;
}
}
}
/**
* Get_show_pic
* Obtain the final image address to be displayed.
*
* @ Param string $ url
* @ Return string
*/
Function get_show_pic ($ url, $ noimg require 'noimg.gif ', $ newimg = "http://special.ku6img.com/projects/2010/d-color/images/slt-1.jpg "){
$ Obj = new RedirectUrl ($ url );
$ Realurl = $ obj-> get_final_url ();
If (strpos ($ realurl, $ noimg)> 0 ){
Return $ newimg;
}
Return $ url;
}
// Reference
Echo "";
?>
The http://www.bkjia.com/PHPjc/327892.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327892.htmlTechArticle code is as follows: // Get the redirected address class RedirectUrl {// address var $ url; // initialize the address function RedirectUrl ($ url) {$ this-url = $ url ;} /*** get_redirect_url ()...