# How to use cURL to Get JSON Data and Decode JSON Data in PHP? (ok)

<figure><img src="/files/2Q6Wi1yTwDde1IxvqA31" alt=""><figcaption></figcaption></figure>

C:\xampp\htdocs\abc\index.php

```php
<?php
// Initializing curl
$curl = curl_init();
// Sending GET request to reqres.in
// server to get JSON data
curl_setopt($curl, CURLOPT_URL,
  "https://reqres.in/api/users?page=2");
// Telling curl to store JSON
// data in a variable instead
// of dumping on screen
curl_setopt($curl,
  CURLOPT_RETURNTRANSFER, true);
// 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
  echo '<pre>';
  var_export($decodedData);
  echo '</pre>';
}
// Closing curl
curl_close($curl);
?>
<?php
// Initializing curl
$curl = curl_init();
// Sending GET request to reqres.in
// server to get JSON data
curl_setopt($curl, CURLOPT_URL,
  "http://localhost/abc/data.json");
// Telling curl to store JSON
// data in a variable instead
// of dumping on screen
curl_setopt($curl,
  CURLOPT_RETURNTRANSFER, true);
// 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
  echo '<pre>';
  var_export($decodedData);
  echo '</pre>';
}
// Closing curl
curl_close($curl);
?>
```

C:\xampp\htdocs\abc\data.json

```javascript
{
	"page": 2,
	"per_page": 6,
	"total": 12,
	"total_pages": 2,
	"data": [
	{
		"id": 7,
		"email": "michael.lawson@reqres.in",
		"first_name": "Michael",
		"last_name": "Lawson",
		"avatar": "https://reqres.in/img/faces/7-image.jpg"
	},
	{
		"id": 8,
		"email": "lindsay.ferguson@reqres.in",
		"first_name": "Lindsay",
		"last_name": "Ferguson",
		"avatar": "https://reqres.in/img/faces/8-image.jpg"
	},
	{
		"id": 9,
		"email": "tobias.funke@reqres.in",
		"first_name": "Tobias",
		"last_name": "Funke",
		"avatar": "https://reqres.in/img/faces/9-image.jpg"
	},
	{
		"id": 10,
		"email": "byron.fields@reqres.in",
		"first_name": "Byron",
		"last_name": "Fields",
		"avatar": "https://reqres.in/img/faces/10-image.jpg"
	},
	{
		"id": 11,
		"email": "george.edwards@reqres.in",
		"first_name": "George",
		"last_name": "Edwards",
		"avatar": "https://reqres.in/img/faces/11-image.jpg"
	},
	{
		"id": 12,
		"email": "rachel.howell@reqres.in",
		"first_name": "Rachel",
		"last_name": "Howell",
		"avatar": "https://reqres.in/img/faces/12-image.jpg"
	}],
	"support":
	{
		"url": "https://reqres.in/#support-heading",
		"text": "To keep ReqRes free, contributions towards server costs are appreciated!"
	}
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learnphp.gitbook.io/learnphp/how-to-use-curl-to-get-json-data-and-decode-json-data-in-php-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
