php cURL没有关注重定向

时间:2014-12-01 23:09:41

标签: php redirect curl http-status-codes

我使用以下代码获取最终URL的http状态代码(在完成所有重定向后):

$handle = curl_init($url);
            curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($handle, CURLOPT_HEADER, true);
            // we want headers
            //curl_setopt($handle, CURLOPT_NOBODY, true);
            curl_setopt($handle, CURLOPT_TIMEOUT, 5);
            curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($handle, CURLOPT_USERAGENT, 'my bot');
            $response = curl_exec($handle);
            $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
            curl_close($handle);

但是,cURL不遵循重定向。 F.E.如果我使用www.raffiniert.biz/kunden,它会显示一个简单的301,而不是关注www.raffiniert.biz/kunden /

怎么了?

由于 圣拉斐尔

1 个答案:

答案 0 :(得分:0)

我使用与您的代码完全相同的预期响应。

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 01 Dec 2014 23:19:56 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 309
Connection: keep-alive
Location: http://www.raffiniert.biz/kunden/
Cache-Control: max-age=0
Expires: Mon, 01 Dec 2014 23:19:56 GMT
Vary: Accept-Encoding

HTTP/1.1 403 Forbidden
Server: nginx
Date: Mon, 01 Dec 2014 23:19:57 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 277
Connection: keep-alive
Vary: Accept-Encoding

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /kunden/
on this server.</p>
<hr>
<address>Apache Server at www.raffiniert.biz Port 80</address>
</body></html>

你必须检查......

error_reporting(-1);

if (ini_get('safe_mode') || (string)ini_get('open_basedir') !== '') {
    exit('You have no permission to set CURLOPT_FOLLOWLOCATION');
}
相关问题