php獲取301跳轉URL簡單實例
/**
* 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實現(xiàn)用socket獲取301跳轉地址,可以提取跳轉過程中的url
- php用header函數(shù)實現(xiàn)301跳轉代碼實例
- PHP header()函數(shù)使用詳細(301、404等錯誤設置)
- 關于php curl獲取301或302轉向的網(wǎng)址問題的解決方法
- 301重定向代碼合集(iis,asp,php,asp.net,apache)
- asp.net php asp jsp 301重定向的代碼(集合)
- asp,asp.net,php,jsp下的301轉向代碼
- php 301轉向?qū)崿F(xiàn)代碼
- 解析網(wǎng)站301重定向的實現(xiàn)方法,包括iis,apache,asp,php的方法
- php 實現(xiàn)301重定向跳轉實例代碼
相關文章
用windows下編譯過的eAccelerator for PHP 5.1.6實現(xiàn)php加速的使用方法
用windows下編譯過的eAccelerator for PHP 5.1.6實現(xiàn)php加速的使用方法...2007-09-09Laravel 5.4.36中session沒有保存成功問題的解決
這篇文章主要給大家介紹了關于Laravel 5.4.36中session沒有保存成功問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-02-02Laravel學習教程之model validation的使用示例
這篇文章主要給大家介紹了關于Laravel學習教程之model validation使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-10-10