Posting Form Data with Curl [PHP Code]

To post form data to the server using Curl, you can use one of two command line options: -F (--form) or -d (--data). The -F command-line parameter sends form data with the multipart/form-data content type, and the -d command-line parameter sends form data with the application/x-www-form-urlencoded content type. In this Curl POST Form example, we submit a form to a ReqBin echo URL in the application/x-www-form-urlencoded format. Click Run to execute the Curl POST Form example online and see the results. The PHP code was automatically generated for the Curl POST Form example.
Posting Form Data with Curl [PHP Code] Run
curl -X POST https://reqbin.com/echo/post/form
   -H "Content-Type: application/x-www-form-urlencoded" 
   -d "param1=value1&param2=value2" 
Updated: Viewed: 84098 times
PHP code for Curl POST Form example

PHP code for Curl POST Form Example

This PHP code snippet was generated automatically for the Curl POST Form example.
<< Back to the Curl POST Form example

What is Curl?

Curl is a popular command-line tool used by programmers and administrators that allows you to send requests to the server, submit web forms, and upload files. Curl supports over 25+ protocols, including HTTP and HTTPS, has built-in support for web forms, SSL, user authentication, and HTTP Cookies. Curl works on Linux, Mac, Windows. It is one of the best tools you can find on the market for debugging network requests and API calls.

What is HTTP POST?

The HTTP POST method is one of nine standard Hypertext Transfer Protocol (HTTP) methods. The POST method requests the webserver to receive and process the data contained in the body of the POST message. The POST method is used to send data to the server, upload files and images, as well as submit HTML forms. Unlike GET and HEAD requests, HTTP POST requests can change the server state.

What is HTML Form?

HTML forms collect user input on HTML pages and submit it to a server for processing. For example, a website might display an HTML page with fields for a name and an email address to register a user. The user can enter the required information and then click the Submit button to send the data to the server for processing. HTML forms are created using the <form> tag.

HTML Form Example
<form action="/register" method="post">
  <input type="text" id="name" name="name">
  <input type="email" id="email" name="email">
  <input type="submit" value="Submit">
</form>

How to submit an HTML form using Curl?

To post a web form with Curl, you need to use the -d command-line option and pass the form data as key/value pairs. By default, Curl sends an HTTP POST request and posts the provided form data with the application/x-www-form-urlencoded content type. You can use the -H command-line parameter to post a form with a different content type and pass the desired data type there, for example, 'Content-Type: multipart/form-data.' The -X POST command-line parameter is optional and can be omitted.

Curl can submit web forms in the same way as a browser, making it impossible for the server-side to distinguish from which client the form was submitted. To achieve this, you also need to specify the User-Agent name and HTTP cookies additionally.

Curl POST Form Syntax

The general form of the Curl command for submitting a web form using the -d command line option is as follows:

Curl POST Form Syntax Using -d Option
curl [URL] -d "key1=value1&key2=value2"

A more verbose version of the same request looks like this:

Curl POST Form Syntax (Verbose Version)
curl -X POST [URL]
   -H "Content-Type: application/x-www-form-urlencoded" 
   -d "key1=value1&key2=value2"

Where:
  • -X, --request: HTTP method for communicating with the server.
  • -H, --header: HTTP headers to send to the server with POST request.
  • -d, --data: Data to be sent to the server using a POST request in the form of key/value pairs.

Curl POST Form with multipart/form-data Format

The -F command line option tells Curl to send HTML data to the server in multipart/form-data format:

Curl POST Form with multipart/form-data Format
curl [URL] -F name=John -F [email protected]

To send multiple values using the -F option, use it multiple times on the command line. To upload binaries, precede the parameter value with an @ sign and specify the path to the file.

Curl POST Form Examples

Examples of posting HTML forms using the -d and -F command-line options:

Curl POST form example with -d flag
curl https://reqbin.com/echo/post/form 
   -d "key1=value1&key2=value2"

Curl POST form example with -F flag
curl https://reqbin.com/echo/post/form 
   -F key1=value1
   -F key2=value2
   -F [email protected]

What is multipart/form-data?

Multipart/form-data is one of the most commonly used content types for sending binary data to the server. Multipart means that data is sent to the server in separate parts. Each of the components may have a different content type, file name, and data. The data are separated from each other by a boundary string. Curl sends a multipart request with a specially formatted POST message body as a series of "parts" separated by MIME boundaries.

Multipart/form-data Example
POST /echo/post/form  HTTP/1.1
Host: reqbin.com
Content-Type: multipart/form-data; boundary=---WD9146A
Content-Length: 100

---WD9146A
Content-Disposition: form-data; name="user-name"

John
---WD9146A
Content-Disposition: form-data; name="text-data"; filename="user.txt"
Content-Type: text/plain

[Text data]
---WD9146A
Content-Disposition: form-data; name="html-data"; filename="user.html"
Content-Type: text/html

[HTML data]
---WD9146A--

See also

Generate code snippets for PHP and other programming languages

Convert your Curl POST Form request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.