Curl : * 违反 RFC 2616/10.3.2 并从 POST 切换到 GET
我正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以设置
CURLOPT_POSTREDIR
来配置此行为(curl 中基于 301 位置标头的自动重定向的请求方法):另请参阅: Request #49571 CURLOPT_POSTREDIR not Implement 其中有一些有用的注释,就像设置自定义请求方法一样:
CURLOPT_POSTREDIR
can be set to configure this behaviour (request method for 301 location header based automatic redirects in curl):See as well: Request #49571 CURLOPT_POSTREDIR not implemented which has some useful comments, like setting a custom request method:
来自 HTTP 的最新草案:
我认为 303 See Other 可能也合适。
From the latest draft of HTTP:
I think a 303 See Other might be suitable too.
好吧,面对重新编译 php,因为我的curl 不支持 POSTREDIR,我决定用 JQuery 解决这个问题。希望这对某人有帮助!
Well, faced with recompiling php because my curl didn't support POSTREDIR i settled for solving this with JQuery. Hope this helps someone!