API Returning JSON in PHP
Currently, for data exchange, sites use the JSON format. Let's create an API that returns data in this format.
For example, let our API return an array filled with integers from one parameter to the second:
<?php
header('Content-Type: application/json'); // specify MIME
$arr = range($_GET['num1'], $_GET['num2']);
echo json_encode($arr);
?>
Let's use our API:
<?php
$url = 'http://api.loc/index.php?num1=1&num2=10';
$res = file_get_contents($url);
var_dump($res); // data in JSON format
?>
Let's convert the received data from JSON format to a regular array:
<?php
$url = 'http://api.loc/index.php?num1=1&num2=10';
$res = file_get_contents($url);
$arr = json_decode($res);
var_dump($arr);
?>
Create an API that will return an array of holiday dates in the current year.