PHP cURL POST JSON Data Example

PHP cURL POST JSON Data Example

If you are creating any web application, it is often necessary to send data between different servers. 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.

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

How to POST JSON Data using PHP cURL

Follow the below given 3 simple steps, you can receive or 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

First of all, you need to create the JSON data that you want to send to the server. In this example, you will create a simple JSON object containing two key-value pairs:

$data = array(
    'name' => 'Dev 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, you set the URL to send the request to, specify that 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, you check for any errors using the curl_errno function and print the error message if there is one. If there are no errors, you 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 HTTP servers. By using this tutorial guide, you can easily implement cURL request to send JSON data over HTTP server. Remember to always sanitize and validate any data that you send or receive to ensure the security and integrity of your application.

More 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 *