The Coronavirus API allows developers to fetch real-time COVID-19 statistics such as confirmed cases, recoveries, and deaths. PHP can consume this API and display live global or country-wise data on a webpage.
json_decode()file_get_contents()The API returns JSON data. PHP fetches the response, decodes it into an associative array, and accesses individual values like cases, deaths, and recoveries.
// Fetch global COVID-19 statistics from API
Global COVID-19 Stats";
echo "Cases: " . $data['cases'] . "
";
echo "Deaths: " . $data['deaths'] . "
";
echo "Recovered: " . $data['recovered'] . "
";
?>
// Fetch COVID-19 statistics for a specific country
COVID-19 Stats for {$country}";
echo "Cases: " . $data['cases'] . "
";
echo "Today Cases: " . $data['todayCases'] . "
";
echo "Deaths: " . $data['deaths'] . "
";
echo "Recovered: " . $data['recovered'] . "
";
?>
// Sample JSON response returned by the API
{
"cases":700000000,
"deaths":6900000,
"recovered":670000000,
"todayCases":1200,
"todayDeaths":50
}
The API response is decoded into an array and individual statistics are printed dynamically. Data updates automatically whenever the API data changes.
You can connect this API to charts or dashboards using JavaScript libraries like Chart.js to visualize trends with auto-refresh using AJAX.