php获取301跳转URL简单实例
来源:互联网 发布:php是什么意思啊 编辑:程序博客网 时间:2024/05/17 12:24
/**
* 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.
* 脚本学堂 www.jbxue.com
* @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实现用socket获取php 301跳转地址,可以提取跳转过程中的url
- php获取301跳转URL简单实例
- php获取301跳转URL
- php 获取301跳转后真实的url
- PHP小程序 获取url链接,url不含.html则自动加上并跳转
- 获取URL参数并跳转
- php 301转向的简单跳转
- PHP获取url参数
- 【php】获取当前url
- php获取url
- php获取页面URL
- php获取url
- php获取url
- php获取当前url
- PHP获取URL
- PHP获取当前url
- php获取页面url
- PHP 获取页面URL
- php获取当前url
- XP中安装 WampServer 2.2 后 Apache无法启动问题解决
- php url重定向 页面跳转 代码
- 关于Nginx的server_name
- php实现页面跳转的几种方法(实例分享)
- 几种CRC16计算公式、初始值、标志位等参数汇总
- php获取301跳转URL简单实例
- git 常用命令
- php2html php生成静态页函数
- 异常结构
- 基于PHP生成静态页的实现方法
- 周鸿祎:把自己当成打工的,一辈子都是打工的!
- 互联网进化,未来BAT360们将消失
- 竞品分析总结
- 《黑马程序员》io---properties对象