<?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);
?>