Should I URL-encode POST data?

General Answer

The general answer to your question is that it depends. And you get to decide by specifying what your “Content-Type” is in the HTTP headers.

A value of “application/x-www-form-urlencoded” means that your POST body will need to be URL encoded just like a GET parameter string. A value of “multipart/form-data” means that you’ll be using content delimiters and NOT url encoding the content.

This answer has a much more thorough explanation if you’d like more information.


Specific Answer

For an answer specific to the PHP libraries you’re using (CURL), you should read the documentation here.

Here’s the relevant information:

CURLOPT_POST

TRUE to do a regular HTTP POST.
This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.

CURLOPT_POSTFIELDS

The full data to post in a HTTP “POST” operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ‘;type=mimetype’. This parameter can either be passed as a urlencoded string like ‘para1=val1&para2=val2&…’ or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.

Leave a Comment