[URL要求PHP卷曲时HTTP代码302错误

问题描述 投票:0回答:2

我通过PHP curl_exec请求URL时收到302错误消息,但手动执行时得到了代码200。可能导致结果不同的差异是什么?

假设服务器中有以下代码:

$url = "http://localhost/circle/my_request/susan?confirm_key=c429d674e36ffe1a4f87fd5a17a0200dcfff0884e8bb3801d2a7d559dcc8d8cd1416295160";
$ch = @curl_init($url);
@curl_setopt($ch, CURLOPT_HEADER, true);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
@curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Circle)");
@curl_setopt($ch, CURLOPT_TIMEOUT,60);
$s = @curl_exec($ch);
$curl_info = @curl_getinfo($ch);
$http_code = $curl_info['http_code'];

在这种情况下,$ http_code == 302,或者重定向了所请求的URL。但是,当我手动将URL放置到浏览器中时,它没有进行重定向,而是将我带到正确的位置。

我想念什么吗?这两个请求的结果如何不同?

提前感谢

-----------其他信息----

您好,我在CURLOPT_FOLLOWLOCATION选项设置为true的情况下重新运行了脚本,并且得到了以下输出:

HTTP/1.1 302 Found Date: Tue, 18 Nov 2014 14:36:37 
GMT Server: Apache/2.4.4 (Win64) 
PHP/5.4.12 X-Powered-By: PHP/5.4.12 
Set-Cookie: PHPSESSID=92t2db5qalq4ajie0ols03hik6; 
path=/; HttpOnly Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate,
post-check=0, pre-check=0 Pragma: no-cache X-Account-Management-Status: none 

Location: http://somehost.com/circle Content-Length: 0 

Content-Type: text/html HTTP/1.1 

301 Moved Permanently Date: Tue, 18 Nov 2014 14:36:37 
GMT Server: Apache/2.4.4 (Win64) 

PHP/5.4.12 Location: http://somehost.com/circle/ 

Content-Length: 232 Content-Type: text/html; 
charset=iso-8859-1 HTTP/1.1 200 OK Date: Tue,
18 Nov 2014 14:36:37 GMT Server: Apache/2.4.4 (Win64)
PHP/5.4.12 X-Powered-By: PHP/5.4.12
Set-Cookie: PHPSESSID=hk02sg1327eq78khbka0sf6pk6; 
path=/; HttpOnly Expires: Thu, 19 Nov 1981 08:52:00 
GMT Cache-Control: no-store, no-cache, must-revalidate, 
post-check=0, pre-check=0 Pragma: no-cache X-Account-Management-Status: none 
Transfer-Encoding: chunked Content-Type: text/html; charset=utf-8

我不确定它在说什么,但是我想它已被重定向到http://somehost.com/circle,然后重定向到http://somehost.com/circle。这是正确的吗?

php html libcurl
2个回答
6
投票

可能是脚本将重定向用于支票cookie或可能用于其他功能。

  1. 您可以在$s输出中看到重定向URL。只需在脚本末尾插入echo $s;

  2. 您还可以设置CURLOPT_FOLLOWLOCATION,然后在自动模式下通过重定向将使卷曲卷曲。


0
投票

尝试添加更多选项

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
© www.soinside.com 2019 - 2024. All rights reserved.