php獲取301跳轉(zhuǎn)URL簡單實(shí)例
/**
* get_redirect_url()
* Gets the address that the provided URL redirects to,
* 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\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實(shí)現(xiàn)用socket獲取301跳轉(zhuǎn)地址,可以提取跳轉(zhuǎn)過程中的url
- php用header函數(shù)實(shí)現(xiàn)301跳轉(zhuǎn)代碼實(shí)例
- PHP header()函數(shù)使用詳細(xì)(301、404等錯(cuò)誤設(shè)置)
- 關(guān)于php curl獲取301或302轉(zhuǎn)向的網(wǎng)址問題的解決方法
- 301重定向代碼合集(iis,asp,php,asp.net,apache)
- asp.net php asp jsp 301重定向的代碼(集合)
- asp,asp.net,php,jsp下的301轉(zhuǎn)向代碼
- php 301轉(zhuǎn)向?qū)崿F(xiàn)代碼
- 解析網(wǎng)站301重定向的實(shí)現(xiàn)方法,包括iis,apache,asp,php的方法
- php 實(shí)現(xiàn)301重定向跳轉(zhuǎn)實(shí)例代碼
相關(guān)文章
PHP 傳輸會(huì)話curl函數(shù)的實(shí)例詳解
這篇文章主要介紹了PHP 傳輸會(huì)話curl函數(shù)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09用windows下編譯過的eAccelerator for PHP 5.1.6實(shí)現(xiàn)php加速的使用方法
用windows下編譯過的eAccelerator for PHP 5.1.6實(shí)現(xiàn)php加速的使用方法...2007-09-09Laravel 5.4.36中session沒有保存成功問題的解決
這篇文章主要給大家介紹了關(guān)于Laravel 5.4.36中session沒有保存成功問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02Laravel學(xué)習(xí)教程之model validation的使用示例
這篇文章主要給大家介紹了關(guān)于Laravel學(xué)習(xí)教程之model validation使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-10-10php實(shí)現(xiàn)大文件斷點(diǎn)續(xù)傳下載實(shí)例代碼
這篇文章主要介紹了php實(shí)現(xiàn)大文件斷點(diǎn)續(xù)傳下載實(shí)例,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10