gno*_*sio 82 php post curl get
我尝试从之前的Post请求切换到Get请求.假设它是一个Get但最终会发布一个帖子.
我在PHP中尝试了以下内容:
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, null);
curl_setopt($curl_handle, CURLOPT_POST, FALSE);
curl_setopt($curl_handle, CURLOPT_HTTPGET, TRUE);
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
附加信息:我已经建立了一个用于执行POST请求的连接.这成功完成但稍后当我尝试重用连接并使用上面的setopts切换回GET时,它仍然在内部使用不完整的POST头进行POST.问题是它认为它正在执行GET但最终放置一个没有content-length参数的POST头,并且连接失败并出现411 ERROR.
RC.*_*RC. 105
在执行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;
从说明curl_setopt()
文档的CURLOPT_HTTPGET
(强调):
[将CURLOPT_HTTPGET设置为]
TRUE
以将HTTP请求方法重置为GET.
由于GET是默认值,因此仅在请求方法已更改时才需要这样做.
Bao*_* Le 53
在调用curl_exec($ curl_handle)之前添加此项
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET');
Run Code Online (Sandbox Code Playgroud)
gno*_*sio 37
解决了:问题出在这里:
我将POST
通过两个_CUSTOMREQUEST
与_POST
和_CUSTOMREQUEST
作为坚持POST
,同时_POST
切换到_HTTPGET
.服务器认为标题_CUSTOMREQUEST
是正确的,然后返回411.
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
Run Code Online (Sandbox Code Playgroud)