代碼如下:
//取重定向的地址
class RedirectUrl{
//地址
var $url;
//初始化地址
function RedirectUrl($url){
$this
}
/**
* get_redirect_url()
* 取重定向的地址
*
* @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[
if (!isset($url_parts[
$sock = fsockopen($url_parts[
if (!$sock) return false;
$request = "HEAD "
$request
$request
fwrite($sock
$response =
while(!feof($sock)) $response
fclose($sock);
if (preg_match(
return trim($matches[
} else {
return false;
}
}
/**
* get_all_redirects()
* 取所有重定向地址
*
* @param string $url
* @return array
*/
private function get_all_redirects($url){
$redirects = array();
while ($newurl = $this
if (in_array($newurl
break;
}
$redirects[] = $newurl;
$url = $newurl;
}
return $redirects;
}
/**
* get_final_url()
* 取實際地址
*
* @param string $url
* @return string
*/
function get_final_url(){
$redirects = $this
if (count($redirects)>
return array_pop($redirects);
} else {
return $this
}
}
}
/**
* get_show_pic
* 取最終要顯示的圖片地址
*
* @param string $url
* @return string
*/
function get_show_pic($url
$obj = new RedirectUrl($url);
$realurl = $obj
if(strpos($realurl
return $newimg;
}
return $url;
}
//參考
echo "<img src=
?>
From:http://tw.wingwit.com/Article/program/PHP/201311/21104.html