PHP获取网址302跳转之后的URL实例代码
hetu144人阅读
今天给大家带来获取域名302跳转之后的真实地址方法,什么意思呢?,302就是一种域名跳转协议,比如我们访问a.com如果有302跳转就可以通过a.com直接访问到b.com,具体代码:
function get_head($sUrl){
$oCurl = curl_init();
$header[] = "Content-type: application/x-www-form-urlencoded";
$user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HTTPHEADER,$header);
curl_setopt($oCurl, CURLOPT_HEADER, true);
curl_setopt($oCurl, CURLOPT_NOBODY, true);
curl_setopt($oCurl, CURLOPT_USERAGENT,$user_agent);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($oCurl, CURLOPT_POST, false);
$sContent = curl_exec($oCurl);
$headerSize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
$header = substr($sContent, 0, $headerSize);
curl_close($oCurl);
return $header;
}
通过上面CURL解析出来的Header头信息我们会得到如下信息:
HTTP/1.1 302 Found Date: Fri, 27 Jun 2014 02:47:35 GMT Server: Apache Location: 解析出来跳转的302地址 Cache-Control: max-age=86400 Expires: Sat, 28 Jun 2014 02:47:35 GMT Connection: Keep-Alive Content-Type: text/html; charset=iso-8859-1
评论 | 0 条评论
登录之后才可留言,前往登录