详情页

PHP获取网址302跳转之后的URL实例代码

时间:2024年05月07日

编辑:佚名

PHP获取网址302跳转之后的URL实例代码,如果对你有帮助就看看吧。
今天给大家带来的是域名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解析出来的头信息,我们会得到如下信息:
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
那么我们只需要获取Location的值,也就是302跳转后的URL地址,
教程到此结束,希望对需要的人有所帮助!
相关文章
猜你需要