How To POST JSON Data with PHP cURL

Post JSON data using CURL in PHP; Through this tutorial, you will learn how to post JSON data using CURL in PHP.

When building a web application, it is often necessary to send data between different servers or services. JSON is a popular format for representing data in a structured way, and the cURL library in PHP provides an easy way to send JSON data over HTTP. In this article, we will show you how to POST JSON data with PHP cURL.

How To POST JSON Data with PHP cURL

Follow the following steps to post json data using curl in PHP; is as follows:

  • Step 1: Set up the JSON data
  • Step 2: Set up the cURL request
  • Step 3: Execute the cURL request

Step 1: Set up the JSON data

Before you can send JSON data with PHP cURL, you need to create the data that you want to send. In this example, we will create a simple JSON object containing two key-value pairs:

$data = array(
    'name' => 'John Smith',
    'email' => '[email protected]'
);

$json = json_encode($data);

The json_encode function converts the array into a JSON string.

Step 2: Set up the cURL request

Next, you need to set up the cURL request to send the JSON data. This involves creating a cURL handle and setting the appropriate options.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://example.com/api');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json)
));

In this example, we set the URL to send the request to, specify that we want to receive the response as a string, set the HTTP method to POST, set the JSON data as the request body, and set the Content-Type and Content-Length headers.

Step 3: Execute the cURL request

Once the cURL handle is set up, you can execute the request using the curl_exec function.

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
} else {
    echo 'Response: ' . $response;
}

curl_close($ch);

In this example, we check for any errors using the curl_errno function and print the error message if there is one. If there are no errors, we print the response body using the curl_exec function.

Conclusion

Sending JSON data with PHP cURL is a simple process that can be used to exchange data between different servers or services. By following the steps outlined in this article, you can easily set up a cURL request to send JSON data over HTTP. Remember to always sanitize and validate any data that you send or receive to ensure the security and integrity of your application.

Recommended PHP Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *