如何在 PHP CURL 中从 POST 切换到 GET [英] How to switch from POST to GET in PHP CURL

查看:19
本文介绍了如何在 PHP CURL 中从 POST 切换到 GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从之前的 Post 请求切换到 Get 请求.假设它是一个 Get 但最终会发布一个帖子.

I have tried switching from a previous Post request to a Get request. Which assumes its a Get but eventually does a post.

我在 PHP 中尝试了以下内容:

I tried the following in PHP :

curl_setopt($curl_handle, CURLOPT_POSTFIELDS, null);
curl_setopt($curl_handle, CURLOPT_POST, FALSE);
curl_setopt($curl_handle, CURLOPT_HTTPGET, TRUE);

我错过了什么?

附加信息:我已经建立了一个连接来执行 POST 请求.这成功完成,但稍后当我尝试重用连接并使用上面的 setopts 切换回 GET 时,它仍然最终在内部执行带有不完整 POST 标头的 POST.问题是它认为它在执行 GET 但最终放置了一个没有内容长度参数的 POST 标头,并且连接失败并显示 411 错误.

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.

推荐答案

已解决:问题出在这里:

Solved: The problem lies here:

我通过 _CUSTOMREQUEST_POST 设置了 POST 并且 _CUSTOMREQUEST 持久化为 POST_POST 切换到 _HTTPGET.服务器假定来自 _CUSTOMREQUEST 的标头是正确的,并返回 411.

I set POST via both _CUSTOMREQUEST and _POST and the _CUSTOMREQUEST persisted as POST while _POST switched to _HTTPGET. The Server assumed the header from _CUSTOMREQUEST to be the right one and came back with a 411.

curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');

这篇关于如何在 PHP CURL 中从 POST 切换到 GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆