23

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 www. I'm using Ubuntu 12.04 LTS. The libcurl version for PHP is 7.22.0.

Contents of 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

<?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_POST to false, post is performed and result is displayed. So, is that CURLOPT_POST not required at all? It looks option CURLOPT_POSTFIELDS takes care of sending data via POST without use of CURLOPT_POST option. When I print $_SERVER in test2.php, request method is always set to POST (with or without option CURLOPT_POST).

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

Raptor
  • 53,206
  • 45
  • 230
  • 366
Sanjay Maurya
  • 343
  • 1
  • 3
  • 7
  • 2
    Yes.To set the request method 'post' in curl we set the curl_setopt($ch, CURLOPT_POST, TRUE); and to send data we use a associative array.And if the array is multiminsional then we use json_encode for the inner level. – Abhisek Malakar Nov 04 '14 at 06:07
  • 2
    Have you read the question properly? I'm not asking what these options CURLOPT_POSTFIELDS and CURLOPT_POST do. Have you tried examples that I have mentioned? – Sanjay Maurya Nov 04 '14 at 12:00
  • Pro tip for everyone to use: `CURLOPT_POSTFIELDS` automatically implies `CURLOPT_POST = true`; **but** setting `CURLOPT_POST = true` will erase any previous `CURLOPT_POSTFIELDS = array(/*...*/)`, causing empty body send (tested on PHP7.4). Convert `CURLOPT_POSTFIELDS` value to string to workaround this (`CURLOPT_POSTFIELD = http_build_query(array(/*...*/))`). – Niki Romagnoli Apr 04 '23 at 11:13

2 Answers2

30

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:

Community
  • 1
  • 1
Niklesh_Chauhan
  • 647
  • 5
  • 16
0

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.

mreinsmith
  • 154
  • 3
  • 14