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: 7329 times

What is PHP?

PHP is a general-purpose server-side language for web development. PHP integrates with web development technologies such as HTML, CSS, JavaScript, and SQL. Due to its open nature and huge developer community, PHP has many libraries and frameworks that make it easier and faster to develop web applications. PHP is also a cross-platform language, which allows it to run on various operating systems such as Windows, macOS, Linux, and Unix.

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 HTTP POST?

The HTTP POST request method sends data to the server or creates/updates a resource. POST request utilized while transmitting an HTML form or uploading data to the server. An HTTP POST request can be with or without data. 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.

The PHP Curl Library is mainly used to send requests to PHP, which is a convenient and flexible way to make HTTP requests. The PHP Curl library provides web application developers with an easy and secure way to interact with external servers and APIs.

What is PHP Curl Library?

The PHP Curl Library is a set of functions that 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

// try get IpInfo ASAP! getIpInfo($.noop);