0

I've seen curl_setopt($ch, CURLOPT_POST, 1); mostly written in this way but have noticed that some people express the count() number of post fields instead of 1/true. Why might they be doing this or could it simply be a misunderstanding of the option?

Jonathan
  • 10,936
  • 8
  • 64
  • 79

1 Answers1

1

From the documentation:

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

Seems to me like it's a boolean. The content-type is enforced for POST requests to be sent properly, which are already default that way in HTML forms that use POST, but not in cURL requests - they have to be set up for it (so this flag is the equivalent of method="post" or method="get" depending on which value you give it, >=1 or 0 respectively.

The reason you saw count($data) in this field in some places is to (in my assumption) disable POST when the $data is empty dynamically, so it's 0 when it is empty and >=1 otherwise.

casraf
  • 21,085
  • 9
  • 56
  • 91
  • The answer to [this](http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean) question seems to imply that the default is already `application/x-www-form-urlencoded` so I still can't see how this option has any impact on the request? – Jonathan Oct 31 '14 at 19:27
  • Because that's referring to HTML forms, not a cURL request. cURL requests would be plain GET by default, and this setting turns it into a POST request. The reason you saw `count($data)` in this field in some places is to maybe disable POST when the `$data` is empty, so it's 0 when it is empty and >=1 otherwise. – casraf Nov 01 '14 at 02:23