Create API COVID-19 data using Laravel Lumen and Swagger as documentation

Hanief
Remote Worker Indonesia
4 min readSep 14, 2021

--

Good day friends!

Today we will explore how to create an API that will retrieve the COVID-19 data from the worldometers.info site using Laravel Lumen and Swagger as documentation.

For the case, we will continue the case before (https://medium.com/@haniefhan/how-to-create-cronjob-to-scrape-covid-19-latest-data-using-php-curl-and-xpathdom-330fb37f5b03), which scraping data using PHP native and CLI, we use Laravel Lumen as an framework for API provider.

Ok, first step, let’s install Laravel Lumen on our machine. Open the CLI / Command prompt, execute this (we create project with name “latest_covid”)

composer create-project --prefer-dist laravel/lumen latest_covid

Ups, don’t forget to install composer first on your machine. How? Hmm… maybe I will create the article for it later.😉

Wait a moment until the process is complete. After the process completed, let’s check if Laravel Lumen has been installed correctly with command:

cd latest_covid
php -S localhost:8000 -t public

Let’s check in browser

Alright, service worked nicely.

Before we go to the next step, from the Lumen Laravel documentation, we must “set your application key to a random string” (here). Let’s create it using simple API script in file “/latest_covid/routes/web.php”.

Let’s check in browser (http://localhost:8000/key)

Copy the string and paste it to .env file line 3,

APP_KEY=JCLij5CUroazWoViKCNgBJJVwRdbwe0q

Ok, setting finished.

Next step, let’s migrate the code from our article before (https://medium.com/@haniefhan/how-to-create-cronjob-to-scrape-covid-19-latest-data-using-php-curl-and-xpathdom-330fb37f5b03).

Create file “/latest_covid_lumen/app/Http/Controllers/CovidDataController.php

As you can see on the code above, we migrate function get_web_page and parse_the_number from our PHP native. We create function latest_covid_data to replace our PHP native and we add function top_ten_covid_case to get the data top ten COVID-19 case.

For function latest_covid_data the script mostly same as the old one.

For function top_ten_covid_case after analyze HTML data of the site, we found the top ten country showed after total summary and continent summary in the table. The data started from eighth “tr” tag and ended after 10 data.

For function get_countries, We need it to know list of countries we can use for API latest_covid_data.

Let’s register the function on our route file (/latest_covid/routes/web.php).

Okay, looks good. Let’s open it using browser.

Okay, I think it’s done.😍

Let’s move to creating API Documentation using Swagger.

To use Swagger, we must add it into our project using composer:

composer require "darkaonline/swagger-lume:8.*"

Success! Swagger has been added to our project.

For Swagger run correctly, we must set up some thing on our project (based on darkaonline/swagger-lume documentation)

Open your bootstrap/app.php file and:

  • uncomment this line (around line 26) in Create The Application section:
$app->withFacades();
  • add this line before Register Container Bindings section:
$app->configure('swagger-lume');

add this line in Register Service Providers section:

$app->register(\SwaggerLume\ServiceProvider::class);

Okay, set up complete.

Let’s add some annotations in our project to test is Swagger has installed correctly. Open the file (/app/Http/Controllers/Controller.php) and add this annotation code inside the class:

ps: You can see how to add annotations in PHP file in documentation here.

Add some annotations too on our CovidDataController.php file (/app/Http/Controllers/CovidDataController.php)

To build the Swagger documentation, let’s publish and generate it using this command:

php artisan swagger-lume:publish
php artisan swagger-lume:generate

Don’t forget to run the service

php -S localhost:8000 -t public

Let’s open it using browser.

http://localhost:8000/api/documentation

Alright, API and Documentation ready to consume!

Additional info

Sometimes, I just get blank page when accessed the swagger documentation. If you get the same problem, you can change the command to run the php service using this command:

php -S localhost:8000 public/index.php

And try again to access the documentation using browser. It’s works for me..😁

By the way, you can see this project repository on my Github here.

Thank you for your time to read. Let’s join us again next time as we explore another interesting case! 😉

--

--