Sending POST request with PHP

There are two ways to send POST requests from PHP: the built-in PHP Curl library and a non-CURL method using PHP's native streaming features. The PHP Curl library gives you full control over sending the request but requires additional initialization steps, while the CURL-less method is easier to use but has limited capabilities. In this PHP POST request example, we are sending a POST request using the PHP Curl library. The CURL-less method is provided with detailed explanations at the end of the article. Click Execute to run the PHP POST Request Example online and see the result.
Sending POST request with PHP Execute
<?php
$url = "https://reqbin.com/echo/post/json";

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

$headers = array(
   "Accept: application/json",
   "Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = <<<DATA
{
  "Id": 78912,
  "Customer": "Jason Sweet",
  "Quantity": 1,
  "Price": 18.00
}
DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

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

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

What is PHP Curl Library?

Curl and its core library libcurl were created by Daniel Stenberg and allow you to interact with servers through various types of protocols, including HTTP, HTTPS, FTP, and others. Libcurl supports HTTPS certificates, HTTP GET, POST, PUT, PATCH, and other HTTP methods, FTP file uploads (FTP file upload can also be done with PHP FTP extension), HTTP form based uploads, proxies, cookies, and user authentication. To use PHP Curl functions, you need to install libcurl 7.10.5 or newer and compile PHP with Curl support. The behavior of PHP Curl functions is affected by the curl.cainfo setting in the php.ini file.

What is the HTTP POST request method?

The HTTP POST request method is used to send data to the server. For example, to submit login or contact forms, upload images, or send JSON, XML, or PDF files to a server. The data is passed to the server in the body of the HTTP POST message or in URL parameters. The data type and length are indicated by the Content-Type and Content-Length headers.

How to post requests using the PHP Curl library?

To send requests using the PHP Curl library, you must first initialize it by calling the curl_init() method, after which you can customize the request by calling curl_setopt() with different parameters. The target URL is passed using the CURLOPT_URL parameter. To tell PHP Curl that we want to send a POST request, we can use the CURLOPT_POST option. To pass data to the POST method, we can use the CURLOPT_POSTFIELDS option. A complete example of sending a POST request with the PHP Curl library looks like this:

PHP POST Request 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);
?>

CURL-less method for sending POST requests using PHP

The PHP stream_context_create() method can send POST requests without using Curl. It is less flexible than the PHP Curl library but can be convenient in some situations. To send a POST request with PHP's stream functions, create an $options object with the required HTTP headers, method, and content y and pass it to the stream_context_create() function. You can then get the POST response data from the server by calling the PHP file_get_contents() function. A complete example of sending a POST request with PHP's stream functions looks like this:

PHP Curl-less POST Request Example
<?php
$url = "https://reqbin.com/echo/post/form";

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

$options = array(
  'http' => array(
    'header'  => "Content-type: application/x-www-form-urlencoded",
    'method'  => 'POST',
    'content' => http_build_query($data)
  )
);
$context  = stream_context_create($options);
$resp = file_get_contents($url, false, $context);
echo $resp;
?>

How to post JSON using PHP?

To send JSON data to the server, you must provide a Content-Type: application/json request header and provide the JSON data in the body of the POST message. The Content-Type header allows the server to correctly interpret and process the received data.

PHP POST JSON 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": 1,
  "Customer": "Jhon Smith",
  "Quantity": 10,
  "Price": 20.00
}
DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$resp = curl_exec($curl);
echo $resp;
curl_close($curl);
?>

How to post XML using PHP?

To post an XML data to


the server, you must provide a Content-Type: application/xml request header and provide the XML data in the body of the POST message.

PHP POST XML Example
<?php
$url = "https://reqbin.com/echo/post/xml";

$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/xml",
  "Accept: application/xml",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = <<<DATA
<?xml version="1.0" encoding="utf-8"?>
<Request>
    <Login>login</Login>
    <Password>password</Password>
</Request>
DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$resp = curl_exec($curl);
curl_close($curl);
echo $resp;
?>

How to post HTML Form using PHP?

HTML forms can be submitted with a Content-Type: application/x-www-form-urlencoded request header and form data can be provided as key=value pairs, as shown in the example below.

PHP POST HTML Form Example
<?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;
?>

See also