Posting Form Data in PHP

To post form data in PHP, you can use the built-in PHP Curl library. You need to initialize the PHP Curl library first by calling the curl_init() function. You can pass the target URL during initialization or set it later by calling the curl_setopt() function with the "CURLOPT_URL" parameter. To pass additional HTTP headers to a PHP POST HTML form request, you can call curl_setopt() with the "CURLOPT_HTTPHEADER" parameter. In this PHP Form POST example, we are sending HTML form data to the ReqBin echo URL and specifying the data type in the POST message with the "Content-Type: application/x-www-form-urlencoded" request HTTP header. Click Execute to run the PHP Post Form Example online and see the result.
Posting Form Data in PHP Execute
<?php
$url = "https://reqbin.com/echo/post/form";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Content-Type: application/x-www-form-urlencoded",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = "key1=value1&key2=value2";

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$resp = curl_exec($curl);
curl_close($curl);

echo $resp;
?>
Updated: Viewed: 12604 times

What is PHP?

PHP is an open-source server-side scripting language used for web development. PHP is a widely-used general-purpose language that can be embedded into HTML code. PHP can support multiple databases, including MySQL, PostgreSQL, and SQLite, and can be easily deployed on a wide range of operating systems, including Windows, Linux, and macOS.

What is HTTP POST?

HTTP POST is a request method of a Hypertext Transfer Protocol (HTTP). The POST request is usually used to submit an HTML forms or upload files and images to the server. The HTTP POST request may or may not contain data. The POST data is included in the body of the POST message. The HTTP POST method differs from HTTP GET and HEAD requests in that POST requests can change the server's state.

POST HTML Form Example
POST /echo/post/form HTTP/1.1
Host: reqbin.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
        
key1=value1&key2=value2

In PHP, the main way to send HTTP POST requests is using the PHP Curl Library.

What is PHP Curl Library?

The PHP Curl Library is a set of functions that allow developers to send and receive data using the HTTP and FTP protocols. The main advantage of the PHP Curl library is the ability to execute requests asynchronously, which improves performance and speeds up request processing. Curl makes sending HTTP requests and managing responses easy, including HTTP GET, POST, PUT, PATCH, and other HTTP methods. The Library supports proxy, cookies, user authentication, and HTML forms-based upload. To use the PHP Curl library, you need to install libcurl 7.10.5 or later and compile PHP with Curl support.

How to post request with PHP Curl Library?

The following is an example of sending a POST request to a ReqBin echo URL with JSON data using the Curl Library:

PHP POST Request with Curl Library Example
<?php
$url = "https://reqbin.com/echo/post/json";
  
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  
$data = <<<DATA
{
  "Id": 78912,
  "Customer": "Jason Sweet"
}
DATA;
  
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  
$resp = curl_exec($curl);
echo $resp;
curl_close($curl);
?>

What is HTML Form?

An HTML form is a web page section containing various interactive elements such as text fields, checkboxes, radio buttons, drop-down lists, and others. These elements allow users to enter data or make selections, which can then be sent to a web server for processing. To create HTML forms, you can use the <form> tag. When a user submits a form, the browser sends an HTTP request to the server containing the data entered by the user.

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>

Typically, a form is sent to the server using an HTTP request using the POST or GET method. When using the GET method, the data from the form is passed in the URL string, and when using the POST method, the data is passed in the request body. Form data can be sent to the server in different formats, such as:

  • application/x-www-form-urlencoded is a standard data submission format where the data from the form is passed in the format of key/value pairs within the request body;
  • multipart/form-data is format is used when files need to be submitted along with form data;
  • text/plain is a format in which form data is passed as plain text within the request body.

How to submit form data in PHP?

The following is a step by step example of submitting form data in PHP:

  1. Initialize a Curl session with the "curl_init()" function;
  2. Set the request URL using the "curl_setopt()" function with the "CURLOPT_URL" option;
  3. Set the POST request method using the "curl_setopt()" function with the "CURLOPT_POST" parameter;
  4. Set the request data using the "curl_setopt()" function with the "CURLOPT_POSTFIELDS" parameter. You can pass an array of key-value pairs as data;
  5. Set any additional options you need, such as headers or timeouts, using the "curl_setopt()" function;
  6. Execute the request using the "curl_exec()" function;
  7. Close the Curl session with the curl_close() function.
PHP Post Form Data Example
<?php
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://reqbin.com/echo/post/form');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

$data = array(
  'key1' => 'value1',
  'key2' => 'value2'
);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($curl);
curl_close($curl);

echo $response;
?>

See also