CURLOPT_POST vs. CURLOPT_POSTFIELDS: Is CURLOPT_POST option required?

前端 未结 2 1199
忘掉有多难
忘掉有多难 2020-12-11 00:11

I\'m new to cURL in PHP. I have a question regarding usage of curl options.

Consider two script files: test1.php and test2.php both present in root

相关标签:
2条回答
  • 2020-12-11 00:56

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

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

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

    • How to switch from POST to GET in PHP CURL
    0 讨论(0)
  • 2020-12-11 00:56

    For future reference the API document say this about 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).

    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.

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

    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.

    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.

    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.


    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.

    0 讨论(0)
提交回复
热议问题