如何在 PHP CURL 中从 POST 切换到 GET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1225409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to switch from POST to GET in PHP CURL
提问by gnosio
I have tried switching from a previous Post request to a Get request. Which assumes its a Get but eventually does a post.
我尝试从之前的 Post 请求切换到 Get 请求。假设它是一个 Get 但最终会发布一个帖子。
I tried the following in PHP :
我在 PHP 中尝试了以下内容:
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, null);
curl_setopt($curl_handle, CURLOPT_POST, FALSE);
curl_setopt($curl_handle, CURLOPT_HTTPGET, TRUE);
What am I missing?
我错过了什么?
Additional information: I already have a connection that's setup to do a POST request. That completes successfully but later on when I try to reuse the connection and switch back to GET using the setopts above it still ends up doing a POST internally with incomplete POST headers. The problem is it believes its doing a GET but ends up putting a POST header without the content-length parameter and the connection fails witha 411 ERROR.
附加信息:我已经建立了一个连接来执行 POST 请求。这成功完成,但稍后当我尝试重用连接并使用上面的 setopts 切换回 GET 时,它仍然最终在内部使用不完整的 POST 标头执行 POST。问题是它认为它在执行 GET 但最终放置了一个没有 content-length 参数的 POST 标头,并且连接失败并显示 411 错误。
采纳答案by gnosio
Solved: The problem lies here:
已解决:问题出在这里:
I set POSTvia both _CUSTOMREQUESTand _POSTand the _CUSTOMREQUESTpersisted as POSTwhile _POSTswitched to _HTTPGET. The Server assumed the header from _CUSTOMREQUESTto be the right one and came back with a 411.
我将POST通过双方_CUSTOMREQUEST和_POST和_CUSTOMREQUEST持续的POST同时_POST切换到_HTTPGET。服务器假定来自的标头_CUSTOMREQUEST是正确的,并返回 411。
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
回答by RC.
Make sure that you're putting your query string at the end of your URL when doing a GET request.
确保在执行 GET 请求时将查询字符串放在 URL 的末尾。
$qry_str = "?x=10&y=20"; $ch = curl_init(); // Set query data here with the URL curl_setopt($ch, CURLOPT_URL, 'http://example.com/test.php' . $qry_str); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 3); $content = trim(curl_exec($ch)); curl_close($ch); print $content;
With a POST you pass the data via the CURLOPT_POSTFIELDS option instead of passing it in the CURLOPT__URL. ------------------------------------------------------------------------- $qry_str = "x=10&y=20"; curl_setopt($ch, CURLOPT_URL, 'http://example.com/test.php'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 3); // Set request method to POST curl_setopt($ch, CURLOPT_POST, 1); // Set query data here with CURLOPT_POSTFIELDS curl_setopt($ch, CURLOPT_POSTFIELDS, $qry_str); $content = trim(curl_exec($ch)); curl_close($ch); print $content;
Note from the curl_setopt()docsfor CURLOPT_HTTPGET(emphasis added):
从说明curl_setopt()文档的CURLOPT_HTTPGET(强调):
[Set CURLOPT_HTTPGET equal to]
TRUEto resetthe HTTP request method to GET.
Since GET is the default, this is only necessary if the request method has been changed.
[设置CURLOPT_HTTPGET等于]
TRUE到复位HTTP请求方法得到的。
由于 GET 是默认值,因此只有在更改请求方法时才需要这样做。
回答by Bao Le
Add this before calling curl_exec($curl_handle)
在调用 curl_exec($curl_handle) 之前添加这个
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET');
回答by Albertino Carvalho
CURL request by default is GET, you don't have to set any options to make a GET CURL request.
CURL 请求默认为 GET,您无需设置任何选项即可发出 GET CURL 请求。

