CORONAVIRUS API

PHP Example

The simplest way to get started with PHP

Below you can find a list of sample, pre-made PHP code that you can start using with a simple copy-paste, and adapting it to your needs.

Don't forget to add your API key in the code.


HTML table with all countries data

<?php
// this is your API key that you get after signing up
$api_key = 'YOUR_API_KEY';

// This is the API endpoint
$external_api_url = "https://api.coronavirusapi.dev/countries/?key=".$api_key;
$curl_init = curl_init();
curl_setopt( $curl_init, CURLOPT_URL, $external_api_url);
curl_setopt( $curl_init, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl_init, CURLOPT_CONNECTTIMEOUT, 5 );
$data = curl_exec( $curl_init );
curl_close( $curl_init );

// this is the data that will be returned in an array format
// now you can do whatever you want with the data, with PHP
$data_array = json_decode( $data, true );
?>
    <table>
        <tr>
            <th>Country</th>
            <th>Cases</th>
            <th>Deaths</th>
        </tr>
<?php
// loop through the array of countries
foreach ($data_array as $item){
    ?>
 <tr>
    <td><?php echo $item['country_name'];?></td>
    <td><?php echo $item['cases'];?></td>
    <td><?php echo $item['deaths'];?></td>
  </tr>
        <?php
}
?>
</table>
            

Automatically display the numbers in a text for a specific country

<?php
// this is your API key that you get after signing up
$api_key = 'YOUR_API_KEY';

// This is the API endpoint
$external_api_url = "https://api.coronavirusapi.dev/countries/gb/?key=" . $api_key;
$curl_init = curl_init();
curl_setopt($curl_init, CURLOPT_URL, $external_api_url);
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_init, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($curl_init);
curl_close($curl_init);
// this is the data that will be returned in an array format
// now you can do whatever you want with the data, with PHP
$data_array = json_decode($data, true);
?>

As of today, there are <strong><?php echo number_format($data_array['cases']); ?></strong> coronavirus cases and
<strong><?php echo number_format($data_array['deaths']); ?></strong> deaths in the United Kingdom, and a total of
<strong><?php echo number_format($data_array['total_recovered']); ?></strong> people have recovered from the disease so far.
            

Automatically display the numbers in a text for a specific location

<?php
// this is your API key that you get after signing up
$api_key = 'YOUR_API_KEY';

// This is the API endpoint
$external_api_url = "https://api.coronavirusapi.dev/cities/us/california?key=" . $api_key;
$curl_init = curl_init();
curl_setopt($curl_init, CURLOPT_URL, $external_api_url);
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_init, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($curl_init);
curl_close($curl_init);
// this is the data that will be returned in an array format
// now you can do whatever you want with the data, with PHP
$data_array = json_decode($data, true);
?>

As of today, there are <strong><?php echo number_format($data_array['cases']); ?></strong> coronavirus cases and
<strong><?php echo number_format($data_array['deaths']); ?></strong> deaths in California.
            

By choosing a plan below, you confirm that you have read, understood and accepted our terms and conditions.