Skip to content
Related Articles
Open in App
Not now

Related Articles

How to use cURL to Get JSON Data and Decode JSON Data in PHP ?

Improve Article
Save Article
  • Last Updated : 01 Apr, 2022
Improve Article
Save Article

In this article, we are going to see how to use cURL to Get JSON data and Decode JSON data in PHP.


  • It stands for Client URL.
  • It is a command line tool for sending and getting files using URL syntax.
  • cURL allows communicating with other servers using HTTP, FTP, Telnet, and more.


  • We are going to fetch JSON data from one of free website, which gives JSON data for testing i.e.
  • First, we initialize curl using curl_init() method.
  • Sending GET request to server using curl_setopt() method with CURLOPT_URL to get json data.
  • After that, we have to tell curl to store json data in a variable instead of dumping on screen. This is done by using CURLOPT_RETURNTRANSFER parameter in curl_setopt() function.
  • Execute curl using curl_exec() method.
  • At last, close the curl using curl_close() method.



// Initializing curl
$curl = curl_init();
// Sending GET request to
// server to get JSON data
curl_setopt($curl, CURLOPT_URL,
// Telling curl to store JSON
// data in a variable instead
// of dumping on screen
// Executing curl
$response = curl_exec($curl);
// Checking if any error occurs
// during request or not
if($e = curl_error($curl)) {
    echo $e;
} else {
    // Decoding JSON data
    $decodedData =
        json_decode($response, true);
    // Outputting JSON data in
    // Decoded form
// Closing curl


GET Request using cURL

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!