How to POST 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 send the request header "Content-Type: application/x-www-form-urlencoded" to the server, which tells the server that the body of the POST request contains HTML form data. Click Execute to run the Post Form with PHP Example online and see the result.
How to POST 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: 1422 times

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 form 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

What is PHP Curl Library?

Curl allows you to interact with servers through different types of protocols, including HTTP, HTTPS, FTP, and others. The Libcurl library supports HTTPS certificates, HTTP GET, HTTP POST, PUT, PATCH, and other HTTP methods, proxy, cookies, user authentication, and HTML forms based upload. To use PHP Curl components, you need to install libcurl 7.10.5 or later and compile PHP with Curl support.

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);
var_dump($resp);
curl_close($curl);
?>

What is HTML Form?

HTML Form is a section of an HTML document that includes controls such as text and password input fields, checkboxes, radio buttons, and a Submit button, enclosed in an HTML <form> tag. HTML forms gather user input and send it to a server for processing. An example of an HTML login form.

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>

See also