PHP curl:CURLOPT_URL、CURLOPT_POST 和 CURLOPT_POSTFIELDS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10916233/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:20:44  来源:igfitidea点击:

PHP curl: CURLOPT_URL, CURLOPT_POST, and CURLOPT_POSTFIELDS

phppostcurlget

提问by NightHawk

If I have a URL that looks like this:

如果我有一个如下所示的 URL:

$url = 'http://domain.com/?foo=bar';

And then execute curl as follows:

然后执行 curl 如下:

$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($resource);
curl_close($resource);

I understand that I make this request via GET (default).

我知道我是通过 GET(默认)发出这个请求的。

Now if I set the following option in the same scenario:

现在,如果我在同一场景中设置以下选项:

curl_setopt($resource, CURLOPT_POST, 1);

I understand it uses POST instead of GET, but does it then POST foowith a value of bar? Or would the proper way for that be:

我知道它使用 POST 而不是 GET,但是它是否使用 POSTfoobar?或者正确的方法是:

$url = 'http://domain.com/';
$post = 'foo=bar';
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $post);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($resource);
curl_close($resource);

And what happens if I do this (i.e. submit the value in the URL and via CURLOPT_POSTFIELDS):

如果我这样做(即在 URL 中并通过 CURLOPT_POSTFIELDS 提交值)会发生什么:

$url = 'http://domain.com/?foo=bar';
$post = 'foo=bar';
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $post);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($resource);
curl_close($resource);

How will the data be submitted in that scenario?

在这种情况下如何提交数据?

采纳答案by Steve

The difference between POST and GET is how the server retrieves the data. As you have set CURLOPT_POST to true, the server normally receives the parameters via the CURLOPT_POSTFIELDS value (i.e. the parameters in the HTTP body) and presumably ignore the parameters sent in the URL string - but that really depends on the individual server.

POST 和 GET 之间的区别在于服务器如何检索数据。由于您已将 CURLOPT_POST 设置为 true,服务器通常通过 CURLOPT_POSTFIELDS 值(即 HTTP 正文中的参数)接收参数,并且可能会忽略 URL 字符串中发送的参数 - 但这实际上取决于单个服务器。