Curl : * 违反 RFC 2616/10.3.2 并从 POST 切换到 GET

发布于 2024-12-16 11:36:48 字数 877 浏览 0 评论 0原文

我正在使用curl 发布到脚本。

curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);

但是涉及到 301 重定向,这会导致curl从POST切换到GET。

HTTP/1.1 301 Moved Permanently
< Location: https://myserver.org/php/callback-f.php
< Content-Length: 0
< Date: Wed, 16 Nov 2011 17:21:06 GMT
< Server: lighttpd/1.4.28
* Connection #0 to host myserver.org left intact
* Issue another request to this URL: 'https://myserver.org/php/callback-f.php'
* Violate RFC 2616/10.3.2 and switch from POST to GET
* About to connect() to myserver.org port 443

有谁知道如何防止curl 切换到GET?

I'm using curl to post to a script.

curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);

But there's 301 redirect involved which casues curl to switch from POST to GET.

HTTP/1.1 301 Moved Permanently
< Location: https://myserver.org/php/callback-f.php
< Content-Length: 0
< Date: Wed, 16 Nov 2011 17:21:06 GMT
< Server: lighttpd/1.4.28
* Connection #0 to host myserver.org left intact
* Issue another request to this URL: 'https://myserver.org/php/callback-f.php'
* Violate RFC 2616/10.3.2 and switch from POST to GET
* About to connect() to myserver.org port 443

Does anyone know how I can prevent curl from switching to GET please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绳情 2024-12-23 11:36:48

可以设置 CURLOPT_POSTREDIR 来配置此行为(curl 中基于 301 位置标头的自动重定向的请求方法):

curl_setopt(, CURLOPT_POSTREDIR, 3);

这里 3 告诉curl 模块重定向 301 和 302 请求。

0,1,2,3 是最后一个参数的有效选项。

0->不设置任何行为
1->仅针对 301 重定向使用相同类型的请求进行重定向。
2->仅针对 302 重定向使用相同类型的请求进行重定向。
3->对于 301 和 302 重定向,使用相同类型的请求进行重定向。

另请参阅: Request #49571 CURLOPT_POSTREDIR not Implement 其中有一些有用的注释,就像设置自定义请求方法一样:

curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST"); 

CURLOPT_POSTREDIR can be set to configure this behaviour (request method for 301 location header based automatic redirects in curl):

curl_setopt( , CURLOPT_POSTREDIR, 3);

here 3 tells curl module to redirect both 301 as well as 302 requests.

0,1,2,3 are the valid options for the last argument.

0 -> do not set any behavior
1 -> follow redirect with the same type of request only for 301 redirects.
2 -> follow redirect with the same type of request only for 302 redirects.
3 -> follow redirect with the same type of request both for 301 and 302 redirects.

See as well: Request #49571 CURLOPT_POSTREDIR not implemented which has some useful comments, like setting a custom request method:

curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST"); 
猫七 2024-12-23 11:36:48

来自 HTTP 的最新草案

注意:由于历史原因,用户代理可能会更改请求方法
对于后续请求,从 POST 到 GET。如果这种行为是
如果不希望出现这种情况,可以使用状态代码 307(临时重定向)来代替。

我认为 303 See Other 可能也合适。

From the latest draft of HTTP:

Note: For historic reasons, user agents MAY change the request method
from POST to GET for the subsequent request. If this behavior is
undesired, status code 307 (Temporary Redirect) can be used instead.

I think a 303 See Other might be suitable too.

套路撩心 2024-12-23 11:36:48

好吧,面对重新编译 php,因为我的curl 不支持 POSTREDIR,我决定用 JQuery 解决这个问题。希望这对某人有帮助!

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>

$.post("path/path/callback.php", { "key":"value", "key2":"value2"});

</script>
</head>
hello

</html>

Well, faced with recompiling php because my curl didn't support POSTREDIR i settled for solving this with JQuery. Hope this helps someone!

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>

$.post("path/path/callback.php", { "key":"value", "key2":"value2"});

</script>
</head>
hello

</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文