Copy CodeThe code is as follows:
Take the redirected address
Class redirecturl{
Address
var $url;
Initialize Address
function RedirectURL ($url) {
$this->url = $url;
}
/**
* Get_redirect_url ()
* Take 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 '): $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)) {
return Trim ($matches [1]);
} else {
return false;
}
}
/**
* Get_all_redirects ()
* Take all redirect 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 ()
* Take 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
* Take the last picture address to display
*
* @param string $url
* @return String
*/
function Get_show_pic ($url, $noimg = ' 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 "";
?>
http://www.bkjia.com/PHPjc/327892.html www.bkjia.com true http://www.bkjia.com/PHPjc/327892.html techarticle Copy the code as follows://Take the Redirected address class redirecturl{//Address var $url;//Initialize address function RedirectURL ($url) {$this-url = $url;}/** * g Et_redirect_url () ...