現介紹利用get_headers() 函數獲取http頭
php 自帶的get_headers()取得服務器響應一個 HTTP 請求所發送的所有標頭。 獲取301狀態肯定沒問題。
301定向的例子:
google.com 會301跳轉至 www.google.com
再www.google.com 會302跳轉至 www.google.com.hk
我寫了個php函數 其php函數作用:
輸入 google.com 得到 www.google.com.hk
輸入 www.google.com 得到 www.google.com.hk
輸入 www.google.com.hk 得到 www.google.com.hk
<?php/* @param str $url 查詢 $return str 定向后的url的真實url */function getrealurl($url){ $header = get_headers($url,1); if (strpos($header[0],'301') || strpos($header[0],'302')) { if(is_array($header['Location'])) { return $header['Location'][count($header['Location'])-1]; }else{ return $header['Location']; } }else { return $url; }}$url = 'http://google.com';$url = getrealurl($url);echo '真實的url為:'.$url;//真實的url為:http://www.google.com.hk/?>
新聞熱點
疑難解答