PHP code for Curl POST Form ExampleThis 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.
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:
A more verbose version of the same request looks like this:
- -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:
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:
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.