php - 对 CURLOPT_HTTPGET 和 CURLOPT_POST 选项感到困惑

标签 php curl

我很困惑为什么有两个与设置请求类型相关的选项。他们对我来说似乎是多余的。来自 the PHP manual page对于 curl_setopt():

CURLOPT_POST

TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.

CURLOPT_HTTPGET

TRUE to reset the HTTP request method to GET. Since GET is the default, this is only necessary if the request method has been changed.

如果我理解正确,默认情况下 cURL 发出 GET 请求,但可以通过执行以下操作将请求类型更改为 POST:

curl_setopt($ch, CURLOPT_POST, true);

但是,要变回GET请求,除了要这样做:

curl_setopt($ch, CURLOPT_POST, false);

你也必须这样做?

curl_setopt($ch, CURLOPT_HTTPGET, true);

最佳答案

您需要查看libcurl documentation以进一步了解选项之间的差异,因为它们非常微妙。关键似乎在于您是否在请求之间重用 curl 句柄。

CURLOPT_HTTPGET :强制 HTTP 请求返回使用 GET,如果 POST、HEAD、PUT 等之前使用相同的 curl 句柄。

CURLOPT_POST :使用 "Content-Type: application/x-www-form-urlencoded" header 的常规 POST 请求,这通常是提交 HTML 表单时使用的那种。然后可以选择使用 CURLOPT_HTTPHEADER 覆盖 header 。

关于php - 对 CURLOPT_HTTPGET 和 CURLOPT_POST 选项感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23723759/

相关文章:

php - F5后如何保存页面状态

php - T_CONSTANT_ENCAPSED_STRING

java - Curl Server Sent Event 管道时无输出

curl - Jenkins - 使用 curl 获取最新的工件

PHP 的 =& 运算符

php - 哪个更快/更有效 - 许多小的 MySQL 查询或一个大的 PHP 数组?

PHP 函数式风格

ruby - 在 Mac OS X 上安装 Homebrew 无法连接到 raw.githubusercontent.com 端口 443

ssl - 未签名的 SSL 证书和本地 curl 的 High Sierra 问题

php - curl 错误 60 : SSL Certificate unable to get local issuer certificate:Tried Everything