我正在嘗試獲取服務器重定向網址。我試過使用cURL查找網站重定向的位置?
function http_head_curl($url,$timeout=10)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // in seconds
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
if ($res === false) {
throw new RuntimeException("cURL exception: ".curl_errno($ch).": ".curl_error($ch));
}
return trim($res);
}
echo http_head_curl("http://www.site.com",$timeout=10);
結果是;
HTTP/1.1 301永久移動日期:孫老師,2013年5月12日23時34分22秒GMT 服務器:Apache連接:關閉X供電-者:PHP/5.3.23 的Set-Cookie:PHPSESSID = 0d4b28dd02bd3d8413c92f71253e8b31;路徑= /; HttpOnly X-Pingback:http://site.com/xmlrpc.php內容類型: text/html; charset = UTF-8位置:http://site.com/ HTTP/1.1 200 OK 日期:2013年5月12日23:34:23 GMT服務器:LiteSpeed連接: 關閉X Powered by:PHP/5.3.23 Set-Cookie: PHPSESSID = 630ed27f107c07d25ee6dbfcb02e8dec;路徑= /; HttpOnly X-Pingback:http://site.com/xmlrpc.php Content-Type:text/html; charset = UTF-8
它顯示幾乎所有的頭信息,但沒有顯示它重定向的位置。我如何獲取重定向的頁面網址?
這是顯示它的302重定向嗎? – user198989 2013-05-13 00:06:37
它現在顯示:) – 2013-05-13 00:09:55
如果$ statuscode作爲數組中的一個字符串返回,那麼您可以使用:if($ statusCode [0] == 3) – 2013-05-13 00:53:37