今天给大家带来获取域名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
然后我们只需要获取,Location对于的值就可以了,他对于的就是302跳转之后的URL地址,那么教程就到这里结束了,希望能帮助到需要的人!