php获取301跳转URL简单实例
2013-12-17 06:48
501 查看
这篇文章主要介绍了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.
* 脚本学堂 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
复制代码代码如下:
/**
* 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获取远程URL的实例讲解
- 封装的 php 函数实例 获取客户端IP 获取在线IP 获取url 获取当前站点的访问路径根目录 数字转人民币
- PHP获取当前页面URL函数实例
- php 获取301跳转后真实的url
- php 获取301跳转后真实的url
- 使用jquery获取url及url参数的简单实例
- android之通过URL来获取网络资源并下载资源简单实例
- php获取301跳转URL
- PHP实现一个简单url路由功能实例
- PHP获取当前页面URL函数实例
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- JQuery获取并解析php返回的json格式数据的简单示例
- PHP通过文件路径获取文件名的实例代码
- HttpURLConnection连接网页和获取数据的使用实例
- php调用KyotoTycoon简单实例
- PHP Socket 编程简单实例
- PHP获取关键词和URL在百度的排名
- PHP 获取客户端 IP 地址的方法实例代码