Posting JSON with PHP Curl Library

To post JSON to the server using the PHP Curl library, you first need to initialize the Curl library by calling the curl_init() function and then pass request parameters using the curl_setopt() function. To pass the target URL, use the CURLOPT_URL parameter, and for the request headers, use the CURLOPT_HTTPHEADER parameter. The JSON data can be passed to the request using the CURLOPT_POSTFIELDS parameter. In this PHP Curl POST JSON example, we post JSON to the ReqBin echo URL with additional Accept and Content-Type headers. Click Execute to run the PHP Curl POST JSON 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: 18329 times

What is PHP?

PHP is a server-side scripting language that is widely used in web development programming. PHP supports many protocols and standards, such as HTTP, SMTP, POP3, IMAP, FTP, and XML. PHP can easily integrate with web development technologies such as HTML, CSS, and JavaScript. One of PHP's key advantages is its cross-platform compatibility, which means it can run on multiple operating systems, including macOS, Windows, Linux, and Unix.

What is JSON?

JSON (JavaScript Object Notation) is a standard text format for storing and transmitting data over the network. API clients extensively use the JSON format to send or receive data from a server. JSON represents four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays). JSON file names use the .json extension. JSON is used to exchange data between applications written in many programming languages, including PHP, JavaScript, Java, C++, C#, Go, Python, and many more.

What is HTTP POST?

The HTTP POST request method is used to send data to the server, such as web form data or PDF files, or to create or update resources when communicating over the REST API protocol. The HTTP POST request can be with or without a body. The body of the POST request message carries the data sent to the server. The Content-Type header specifies the data type present within the POST request's body, and the data length is indicated using the HTTP Content-Length header.

What is PHP Curl Library?

The PHP Curl library is a convenient and flexible way to make HTTP requests in PHP. The PHP Curl library provides web application developers with an easy and secure way to interact with external servers and APIs.

The PHP Curl Library 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. 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 make POST requests using the Curl library, follow these steps: initialize a Curl session with curl_init(), set additional parameters with curl_setopt(), start the session using curl_exec(), close the session with curl_close(), and output the returned 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;
?>

How to send JSON using PHP Curl Library?

To send JSON data using the PHP Curl Library, you need to follow these steps:

  1. Create the object or array of data you want to send;
  2. Convert it to JSON format using the json_encode() function;
  3. Initialize Curl with the curl_init() function;
  4. Set Curl options including URL, request type, and JSON data;
  5. Execute a Curl request using the curl_exec() function;
  6. Close the Curl session with the curl_close() function.
PHP POST JSON with Curl Library Example
<?php
$data = array(
    'Id' => 78912,
    'Customer' => 'Jason Sweet'
);

$url = 'https://reqbin.com/echo/post/json';

$curl= curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen(json_encode($data))
));

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

echo $response;
?>

See also