How do I send a POST request using PHP?

There are two options for sending POST requests from PHP: using the built-in Curl library and the CURL-less method that uses PHP's native streaming functions. In this PHP POST request example, we are sending a POST request using the Curl library. The CURL-less method is provided with detailed explanations at the end of the article. The Content-Type: application/json request header specifies the data type in the POST message body, and the Accept: application/json header tells the server that the PHP client is expecting JSON in response to our POST request. Click Execute to run the PHP POST Request Example online and see the result.
How do I send a POST request using 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: 5705 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, to upload images, or to send JSON, XML, or PDF files to a server.

How to post requests using PHP Curl library?

To send requests using the Curl library, you must first initialize it by calling the curl_init() method. The target URL is set by calling the curl_setopt($ curl, CURLOPT_URL, $url) method. To tell PHP Curl that we want to send a POST request, we must call the curl_setopt($curl, CURLOPT_POST, true) method. And to pass POST data to PHP, we must call the curl_setopt($curl, CURLOPT_POSTFIELDS, $data) method. A completed example of sending a POST request with 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);
var_dump($resp);
curl_close($curl);
?>

CURL-less method for sending POST requests using PHP

The PHP stream_context_create() method can be used to send POST requests without Curl. It is not as powerful or simple as Curl, but it may be more convenient in some situations. In order to send a POST request with PHP stream functions, you need to create an $options object with the required POST headers and POST message body and pass it to the stream_context_create(). Then you can get the POST response data from the server by calling the PHP file_get_contents() method. A completed example of sending a POST request with PHP stream functions looks like this:

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

See also