NederlandsKlik deze knop voor de Nederlandstalige website

COVID-19 CORONA Get data with PHP

COVID-19 CORONA Requesting data is easy with PHP and cURL. In the previous article, you can read how to retrieve the data from the Coronavirus Disease (COVID-19) GIS Hub REST API with an ESP32 microcontroller and a sketch programmed in the Arduino IDE. At the request of a number of readers, there is now a simple example of how you can do the same with the PHP programming language. This way you can then use the data in, for example, a website or app.

Coronavirus COVID-19 Global Cases
Coronavirus COVID-19 Global Cases

COVID-19 GIS Hub REST API

This time we will also retrieve the data from the Coronavirus Disease GIS Hub REST API. You can request the data in JSON format there. Using the API-Explorer on that website, you can easily compile the correct query URL.

COVID-19 CORONA Get data with PHP - REST API Query-URL
COVID-19 CORONA Get data with PHP – REST API Query-URL

We choose the following settings:

  • For “Country_Region” choose for example “Indonesia”
  • At “From fields” only check “Last_Update”, “Confirmed”, “Deaths”, “Recovered”
  • Under “Output options” set “Request geometry” to “False” (and the rest too)

At the top right, see the obtained query URL:

https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?where=UPPER(Country_Region)%20like%20%27%25INDONESIA%25%27&outFields=Last_Update,Recovered,Deaths,Confirmed&returnGeometry=false&outSR=4326&f=json

Click “Try Now” to test the settings, you will see approximately the following JSON code:

{
   "objectIdFieldName": "OBJECTID",
   "uniqueIdField": {
      "name": "OBJECTID",
      "isSystemMaintained": true
   },
   "globalIdFieldName": "",
   "geometryType": "esriGeometryPoint",
   "spatialReference": {
      "wkid": 4326,
      "latestWkid": 4326
   },
   "fields": [
      {
         "name": "Last_Update",
         "type": "esriFieldTypeDate",
         "alias": "Last Update",
         "sqlType": "sqlTypeOther",
         "length": 8,
         "domain": null,
         "defaultValue": null
      },
      {
         "name": "Recovered",
         "type": "esriFieldTypeInteger",
         "alias": "Recovered",
         "sqlType": "sqlTypeOther",
         "domain": null,
         "defaultValue": null
      },
      {
         "name": "Deaths",
         "type": "esriFieldTypeInteger",
         "alias": "Deaths",
         "sqlType": "sqlTypeOther",
         "domain": null,
         "defaultValue": null
      },
      {
         "name": "Confirmed",
         "type": "esriFieldTypeInteger",
         "alias": "Confirmed",
         "sqlType": "sqlTypeOther",
         "domain": null,
         "defaultValue": null
      }
   ],
   "features": [
      {
         "attributes": {
            "Last_Update": 1584528782000,
            "Recovered": 11,
            "Deaths": 19,
            "Confirmed": 227
         }
      }
   ]
}

We will paste this URL into the PHP code below.

PHP Code: Request JSON data

PHP needs the cURL extension (“Client URL Library”) to make an API request. The PHP excerpt below shows how to get the JSON data from the REST API with cURL.

  • the URL obtained above is assigned to the variable $url
  • then the API is addressed with curl_exec
  • the resulting JSON string ends up in the variable $response
  • then json_decode converts the JSON string to a PHP variable and maps it to $result
  • finally, the contents of $result are printed using var_dump ()
<?php

$url = "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?where=UPPER(Country_Region)%20like%20%27%25INDONESIA%25%27&outFields=Last_Update,Recovered,Deaths,Confirmed&returnGeometry=false&outSR=4326&f=json";

$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);

$result = json_decode($response);

echo"<pre>";
var_dump($result);
echo"</pre>";

?>

The result is the object below:

object(stdClass)#1 (7) {
  ["objectIdFieldName"]=>
  string(8) "OBJECTID"
  ["uniqueIdField"]=>
  object(stdClass)#2 (2) {
    ["name"]=>
    string(8) "OBJECTID"
    ["isSystemMaintained"]=>
    bool(true)
  }
  ["globalIdFieldName"]=>
  string(0) ""
  ["geometryType"]=>
  string(17) "esriGeometryPoint"
  ["spatialReference"]=>
  object(stdClass)#3 (2) {
    ["wkid"]=>
    int(4326)
    ["latestWkid"]=>
    int(4326)
  }
  ["fields"]=>
  array(4) {
    [0]=>
    object(stdClass)#4 (7) {
      ["name"]=>
      string(11) "Last_Update"
      ["type"]=>
      string(17) "esriFieldTypeDate"
      ["alias"]=>
      string(11) "Last Update"
      ["sqlType"]=>
      string(12) "sqlTypeOther"
      ["length"]=>
      int(8)
      ["domain"]=>
      NULL
      ["defaultValue"]=>
      NULL
    }
    [1]=>
    object(stdClass)#5 (6) {
      ["name"]=>
      string(9) "Recovered"
      ["type"]=>
      string(20) "esriFieldTypeInteger"
      ["alias"]=>
      string(9) "Recovered"
      ["sqlType"]=>
      string(12) "sqlTypeOther"
      ["domain"]=>
      NULL
      ["defaultValue"]=>
      NULL
    }
    [2]=>
    object(stdClass)#6 (6) {
      ["name"]=>
      string(6) "Deaths"
      ["type"]=>
      string(20) "esriFieldTypeInteger"
      ["alias"]=>
      string(6) "Deaths"
      ["sqlType"]=>
      string(12) "sqlTypeOther"
      ["domain"]=>
      NULL
      ["defaultValue"]=>
      NULL
    }
    [3]=>
    object(stdClass)#7 (6) {
      ["name"]=>
      string(9) "Confirmed"
      ["type"]=>
      string(20) "esriFieldTypeInteger"
      ["alias"]=>
      string(9) "Confirmed"
      ["sqlType"]=>
      string(12) "sqlTypeOther"
      ["domain"]=>
      NULL
      ["defaultValue"]=>
      NULL
    }
  }
  ["features"]=>
  array(1) {
    [0]=>
    object(stdClass)#9 (1) {
      ["attributes"]=>
      object(stdClass)#8 (4) {
        ["Last_Update"]=>
        int(1584528782000)
        ["Recovered"]=>
        int(11)
        ["Deaths"]=>
        int(19)
        ["Confirmed"]=>
        int(227)
      }
    }
  }
}

The data we are looking for is in the last piece of data:

  ["features"]=>
  array(1) {
    [0]=>
    object(stdClass)#9 (1) {
      ["attributes"]=>
      object(stdClass)#8 (4) {
        ["Last_Update"]=>
        int(1584528782000)
        ["Recovered"]=>
        int(11)
        ["Deaths"]=>
        int(19)
        ["Confirmed"]=>
        int(227)
      }
    }
  }

With the code below we get the desired data from the object:

$Confirmed = $result->features[0]->attributes->Confirmed;

$Deaths = $result->features[0]->attributes->Deaths;

$Recovered = $result->features[0]->attributes->Recovered;

We still have to convert the (Epoch) timestamp to a legible date and time:

$unix_timestamp = $result->features[0]->attributes->Last_Update;

$Last_Update = date("l d F Y, H:i:s", substr($unix_timestamp, 0, 10));

If we put all the above code together, the code below is the result:

<?php

$url = "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?where=UPPER(Country_Region)%20like%20%27%25INDONESIA%25%27&outFields=Last_Update,Recovered,Deaths,Confirmed&returnGeometry=false&outSR=4326&f=json";

$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);

$result = json_decode($response);

$Confirmed = $result->features[0]->attributes->Confirmed;
$Deaths = $result->features[0]->attributes->Deaths;
$Recovered = $result->features[0]->attributes->Recovered;

$unix_timestamp = $result->features[0]->attributes->Last_Update;
$Last_Update = date("l d F Y, H:i:s", substr($unix_timestamp, 0, 10)); // 13 digit epoch time to date conversion

echo"Confirmed: ".$Confirmed;
echo"<br>";
echo"Deaths: ".$Deaths;
echo"<br>";
echo"Recovered: ".$Recovered;
echo"<br>";
echo"Last Update: ".$Last_Update;

?>

The code example is also available on GitHub.

The output of that code is then as the below text:

Confirmed: 227
Deaths: 19
Recovered: 11
Last Update: Wednesday 18 March 2020, 11:53:02

COVID-19 CORONA Request data with PHP: your project

As with most articles on this blog, this information is only an impetus for creating your own project. If you have made something beautiful yourself, please let us know in the comments under this blog!

COVID-19 CORONA Get data: Kawal Corona

On request, I made an example for the API on KAWAL CORONA, a website of Ethical Hacker Indonesia. The code example is also available on GitHub.

<?php

$url = "https://api.kawalcorona.com/indonesia/";

$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);

$result = json_decode($response);

$Confirmed = $result[0]->positif;

$Deaths = $result[0]->meninggal;

$Recovered = $result[0]->sembuh;

$datetimeString = $result[1]->lastupdate;
$Last_Update = date("l d F Y, H:i:s", strtotime($datetimeString));

echo "Confirmed: ".$Confirmed;
echo "<br>";
echo "Deaths: ".$Deaths;
echo "<br>";
echo "Recovered: ".$Recovered;
echo "<br>";
echo "Last Update: ".$Last_Update;

?>
kawalcorona.com
kawalcorona.com

Worldwide totals

Again on request, I have figured out how you can request the totals. With the API explorer, you can only retrieve statistics per country or region. Querying the global totals, therefore, requires some creativity.

The map on Coronavirus COVID-19 Global Cases does show the totals, but if you try the XHR requests that are used for that, you will run into 403 errors. However, by combining the hosts that the API explorer indicates with the queries that the Global Cases map uses, it works. This will yield the following URLs:

Worldwide number of infections:

https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&outStatistics=%5B%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22Confirmed%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&cacheHint=true

Worldwide number of deaths:

https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&outStatistics=%5B%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22Deaths%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&cacheHint=true

Worldwide recovered:

https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&outStatistics=%5B%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22Recovered%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&cacheHint=true

Php code example:

<?php

$confirmed = getData("https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&outStatistics=%5B%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22Confirmed%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&cacheHint=true");

$deaths = getData("https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&outStatistics=%5B%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22Deaths%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&cacheHint=true");

$recovered = getData("https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/Coronavirus_2019_nCoV_Cases/FeatureServer/1/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&outStatistics=%5B%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22Recovered%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&cacheHint=true");

echo"Confirmed: ".$confirmed->features[0]->attributes->value;
echo"<br>";
echo"Deaths: ".$deaths->features[0]->attributes->value;
echo"<br>";
echo"Recovered: ".$recovered->features[0]->attributes->value;

function getData($url){

   $client = curl_init($url);
   curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
   $response = curl_exec($client);

   return json_decode($response);
};

?>

 

8 Comments

Add a Comment

Your email address will not be published. Required fields are marked *