简体   繁体   English

php cURL不遵循重定向

[英]php cURL doesn't follow redirect

I use the following code to get the http-status code of the final URL (after following all redirects): 我使用以下代码来获取最终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);

However, cURL doesn't follow the redirects. 但是,cURL不遵循重定向。 Fe if I use www.raffiniert.biz/kunden, it shows a plain 301, instead of following to www.raffiniert.biz/kunden/ 如果我使用www.raffiniert.biz/kunden,它显示的是普通301,而不是跟随www.raffiniert.biz/kunden/

What's wrong? 怎么了?

Thanks Raphael 谢谢拉斐尔

I got expected response using exactly the same as your code. 我得到的响应与您的代码完全相同。

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>

You have to check... 你必须检查...

error_reporting(-1);

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM