php CURLOPT_POST 与 CURLOPT_POSTFIELDS:是否需要 CURLOPT_POST 选项?

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

CURLOPT_POST vs. CURLOPT_POSTFIELDS: Is CURLOPT_POST option required?

phpcurllibcurl

提问by Sanjay Maurya

I'm new to cURLin PHP. I have a question regarding usage of curl options.

我是PHP cURL 的新手。我有一个关于使用 curl 选项的问题。

Consider two script files: test1.php and test2.php both present in root www. I'm using Ubuntu 12.04 LTS. The libcurl version for PHP is 7.22.0.

考虑两个脚本文件:test1.php 和 test2.php,它们都存在于根 www 中。我正在使用Ubuntu 12.04 LTS。PHP 的 libcurl 版本是7.22.0

Contents of test1.php

test1.php 的内容

<?php
    $ch = curl_init();
    $post_data = array(
        'firstname' => 'John',
        'lastname' => 'Doe'
    );
    curl_setopt($ch, CURLOPT_URL, 'localhost/test2.php');
    curl_setopt($ch, CURLOPT_POST, TRUE);   //is it optional?
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_exec($ch);
    curl_close($ch);
?>

Contents of test2.php

test2.php 的内容

<?php 
    var_dump($_POST);
?>

When I execute test1.php via browser, I can see results posted. Now if I remove curl option containing CURLOPT_POST, the example still works. Even if I set CURLOPT_POSTto false, post is performed and result is displayed. So, is that CURLOPT_POSTnot required at all? It looks option CURLOPT_POSTFIELDStakes care of sending data via POSTwithout use of CURLOPT_POSToption. When I print $_SERVERin test2.php, request method is always set to POST(with or without option CURLOPT_POST).

当我通过浏览器执行 test1.php 时,我可以看到发布的结果。现在,如果我删除包含CURLOPT_POST 的curl 选项,该示例仍然有效。即使我将CURLOPT_POST 设置为 false,也会执行 post 并显示结果。那么,CURLOPT_POST根本不需要吗?它看起来选项CURLOPT_POSTFIELDS负责通过POST发送数据而不使用CURLOPT_POST选项。当我$_SERVER在 test2.php 中打印时,请求方法总是设置为POST(有或没有选项CURLOPT_POST)。

Could anyone please let me know the exact use of CURLOPT_POSToption? Is it neccessary required for sending data via POST?

任何人都可以让我知道CURLOPT_POST选项的确切用法吗?是否需要通过 发送数据POST

回答by Niklesh_Chauhan

You are correct. CURLOPT_POSTFIELDSimplies CURLOPT_POST. You don't need to use CURLOPT_POSTwhile using CURLOPT_POSTFIELDS. The request method will always be set to POST in this case.

你是对的。CURLOPT_POSTFIELDS意味着CURLOPT_POST。使用CURLOPT_POSTFIELDS时不需要使用CURLOPT_POST。在这种情况下,请求方法将始终设置为 POST。

Note that this is in your case as long as you want it to be a POSTrequest.

请注意,只要您希望它是POST请求,这就是您的情况。

If you don't want to be it a POSTrequestbut set CURLOPT_POSTFIELDS, please see this related Q&A:

如果您不想成为POST请求而是设置CURLOPT_POSTFIELDS,请参阅此相关问答:

回答by mreinsmith

For future reference the API document say this about CURLOPT_POST

为了将来参考 API 文档说这个关于 CURLOPT_POST



Summary:

概括:

A true parameter tells the library to do a regular HTTP post. This will also make the library use the a "Content-Type: application/x-www-form-urlencoded" header. (This is by far the most commonly used POST method).

true 参数告诉库执行常规 HTTP 发布。这也将使库使用“Content-Type: application/x-www-form-urlencoded”标头。(这是迄今为止最常用的 POST 方法)。

Use the CURLOPT_POSTFIELDS option to specify what data to post and CURLOPT_POSTFIELDSIZE to set the data size. Optionally, you can provide data to POST using the CURLOPT_READFUNCTION and CURLOPT_READDATA options.

使用 CURLOPT_POSTFIELDS 选项指定要发布的数据和 CURLOPT_POSTFIELDSIZE 来设置数据大小。或者,您可以使用 CURLOPT_READFUNCTION 和 CURLOPT_READDATA 选项向 POST 提供数据。

You can override the default POST Content-Type: header by setting your own with CURLOPT_HTTPHEADER.

您可以通过使用 CURLOPT_HTTPHEADER 设置您自己的标头来覆盖默认的 POST Content-Type: 标头。

Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue" header. You can disable this header with CURLOPT_HTTPHEADER as usual.

在 HTTP 1.1 中使用 POST 意味着使用“Expect: 100-continue”标头。您可以像往常一样使用 CURLOPT_HTTPHEADER 禁用此标头。

If you use POST to a HTTP 1.1 server, you can send data without knowing the size before starting the POST if you use chunked encoding. You enable this by adding a header like "Transfer-Encoding: chunked" with CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must specify the size in the request.

如果您使用 POST 到 HTTP 1.1 服务器,如果您使用分块编码,您可以在不知道大小的情况下在开始 POST 之前发送数据。您可以通过使用 CURLOPT_HTTPHEADER 添加诸如“Transfer-Encoding: chunked”之类的标头来启用此功能。使用 HTTP 1.0 或不使用分块传输,您必须在请求中指定大小。

if you have issued a POST request and want to make a HEAD or GET instead, you must explicitly pick the new request type using CURLOPT_NOBODY or CURLOPT_HTTPGET or similar.

如果您已发出 POST 请求并希望改为发出 HEAD 或 GET,则必须使用 CURLOPT_NOBODY 或 CURLOPT_HTTPGET 或类似方法显式选择新的请求类型。



I'm testing right now whether setting the CURLOPT_POST to try will override my CURLOPT_HTTPHEADER, "Content-Type: application/json; charset=utf-8" setting.

我现在正在测试将 CURLOPT_POST 设置为 try 是否会覆盖我的 CURLOPT_HTTPHEADER, "Content-Type: application/json; charset=utf-8" 设置。