All Tutorials

Your One-Stop Destination for Learning and Growth

Sending and Receiving JSON Data with PHP and Curl

JSON (JavaScript Object Notation) is a popular data format with diverse uses in building dynamic web applications. In this tutorial, we will discuss how to send and receive JSON data using PHP and the CURL library.

Prerequisites

Before we begin, make sure you have a good understanding of:

  1. PHP (Hypertext Preprocessor) - a popular server-side scripting language.
  2. CURL (Client for URLs) - a command-line tool for transferring data with various protocols, such as HTTP and HTTPS.

Setting up the Environment

First, let's create a new PHP file named json_curl.php. This will be our project file where we will write the code for sending and receiving JSON data using CURL.

Sending JSON Data

To send JSON data with PHP and CURL, follow these steps:

  1. Create a JSON string in PHP using the json_encode() function.
  2. Initialize a new CURL session.
  3. Set the URL, HTTP method (POST or PUT), headers, and data (if needed).
  4. Send the request and receive the response.
<?php
// Create JSON data as an associative array
$jsonData = [
    "name" => "John Doe",
    "age" => 30
];

// Encode the JSON data
$jsonString = json_encode($jsonData);

// Initialize a new CURL session
$ch = curl_init();

// Set URL, HTTP method, and headers
curl_setopt($ch, CURLOPT_URL, "https://example.com/api");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Accept: application/json'
]);

// Set data
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonString);

// Send the request and receive the response
$response = curl_exec($ch);

// Close the CURL session
curl_close($ch);

// Decode the JSON response and print it
print_r(json_decode($response));
?>

Receiving JSON Data

To receive JSON data with PHP and CURL, follow these steps:

  1. Initialize a new CURL session.
  2. Set the URL and HTTP method (GET or DELETE).
  3. Send the request and receive the response.
  4. Decode the JSON response and handle it as needed.
<?php
// Initialize a new CURL session
$ch = curl_init();

// Set URL and HTTP method
curl_setopt($ch, CURLOPT_URL, "https://example.com/api/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Send the request and receive the response
$response = curl_exec($ch);

// Close the CURL session
curl_close($ch);

// Decode the JSON response and handle it as needed
$data = json_decode($response);
print_r($data->name); // Output: John Doe
?>

Conclusion

In this tutorial, we have learned how to send and receive JSON data using PHP and CURL. This knowledge will be useful in developing applications that rely on APIs for data exchange or when working with web services that use JSON format. By understanding the concepts presented here, you'll be able to create more dynamic and versatile applications.

Now, give it a try by writing your code and sending some requests! Good luck, and happy coding!

Published September, 2017