gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:26:48 25 4
gpt4 key购买 nike

我很困惑为什么有两个与设置请求类型相关的选项。他们对我来说似乎是多余的。来自 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com