Posting JSON with PHP Curl Library

To post JSON to the server using the PHP Curl library, you must first initialize the PHP Curl library by calling curl_init() and then pass the target URL by calling curl_setopt() with the "CURLOPT_URL" parameter. JSON data can be passed to PHP Curl by calling curl_setopt($curl, CURLOPT_POSTFIELDS, $data). To pass additional HTTP headers to the PHP POST request, you can call curl_setopt() with the CURLOPT_HTTPHEADER parameter. In this PHP Curl POST JSON example, we send JSON along with the "Accept: application/json" and "Content-Type: application/json" request headers. The "Content-Type: application/json" request header specifies the type of data in the body of the POST message, and the "Accept: application/json" header tells the server that the client is expecting JSON. Click Execute to run the POST JSON with PHP Curl Library Example online and see the result.
Posting JSON with PHP Curl Library Execute
<?php
$url = "https://reqbin.com/echo/post/json";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$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: 6213 times

What is JSON?

JSON (JavaScript Object Notation) is a text-based, language-independent format for storing and transferring data over a network. JSON reaches from JavaScript and has a syntax analogous to JavaScript but can be used separately from it. JSON is used for client-server communication in mobile and web applications written in many programming languages, including PHP, JavaScript, Python, Java, C++, C#, Go, and many others.

What is PHP Curl Library?

Curl allows you to interact with servers through various types of protocols, including HTTP, HTTPS, FTP, and others. The Libcurl library supports HTTPS certificates, HTTP GET, POST, PUT, PATCH, and other HTTP methods, FTP file upload (FTP file upload can also be done with PHP FTP extension), proxy, cookies, user authentication, and HTML forms based upload. PHP Curl library is an extension of PHP that allows making HTTP requests. To use PHP Curl features, you need to install libcurl 7.10.5 or later and compile PHP with Curl support. The behavior of the PHP Curl functions is affected by the curl.cainfo setting in the php.ini file.

How to make POST requests using the PHP Curl Library?

To use the Curl library to send POST requests, you can follow these steps:

  • Initialize a Curl session with function curl_init()
  • Pass additional parameters wih the curl_setopt() function:
    • Target URL with the "CURLOPT_URL" option.
    • Additional HTTP headers with the "CURLOPT_HEADER" parameter.
    • Data with "CURLOPT_POSTFIELDS" parameter.
    • Set the "CURLOPT_RETURNTRANSFER" to "true" if need to return the string instead of printing it out
  • Start a Сurl session with the curl_exec() function
  • Close the Сurl session we created with the curl_close() function
  • Output the return string
PHP POST Curl Example
<?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_HTTPHEADER, array("Accept: application/json"));
curl_setopt($curl, CURLOPT_POSTFIELDS, "POST DATA");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

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

echo $resp;
?>

See also