Building Earthquake API with Laravel

Building Earthquake API with Laravel
Building Earthquake API with Laravel 8.x

Laravel is the most powerful framework built with PHP. It has lots of ready-to-use features that you can use. You can visit the website to see all the details about it. Today I will use it to create an earthquake API service.

In this tutorial, I will create a data scraper to scrape earthquake data then create an API that serves earthquake data from Turkey.

I will follow these steps while I’m building this API service:

  1. Preparation
    1. Installing and Configuring Laravel
    2. Inspecting the Data Source
    3. Create Migrations to Create Database and Tables
    4. Create Models to Save, Serve and Manipulate Data
  2. Scraping Data for Earthquake API
  3. Creating Earthquake API
    1. Create a Controller to Serve Data
    2. Create Route for The API Interface

Preparation

Installing and Configuring Laravel

We need the Composer to install Laravel. You can visit this URL to learn how to install it. Then we need to run this command in the command line.

composer create-project laravel/laravel earthquake-api

This code will create a directory with the name earthquake-API then put Laravel files into it.

I will create a database in MySQL named earthquake. If you do not have MySQL service on your computer or server you need to install it. You can check this URL on how to do it.

In .env we need to change MySQL parameters with ours. Like database name, MySQL username, and password. Here is how the .env file’s database section looks in the default

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Inspecting the Data Source

Before creating the migration file we need to inspect what data we can scrape from the source website. This will define our columns to keep data. I checked that website then we need these columns like this:

Earthquakes data from Turkey

Create Migrations to Create Database and Tables

I will create just one migration file to keep the earthquakes. In the earthquake-api directory, we need to run this command to create a migration file that creates a database table which we need.

php artisan make:migration create_earthquakes_table
----------------------------------------------------
//Result : Created Migration: 2021_05_14_154256_create_earthquakes_table

We can find this file under earthquake-api/database/migrations/2021_05_14_154256_create_earthquakes_table.php

Here is the content of the 2021_05_14_154256_create_earthquakes_table.php file

After editing this migration file we need to run a command that I wrote below. This command will run our migration files to create/update/delete etc. database tables.

php artisan migrate
----------------------------------------------------
Result: Migrated ........_table

This means all migrations ran successfully. So, the database tables were created successfully.

Create Models to Save, Serve and Manipulate Data

We need a model that is named “Earthquake”. You can run this command to create it.

php artisan make:model Models\Earthquake
----------------------------------------------------
Result: Model created successfully.

You can see this model file under earthquake-api/app/models/Earthquake.php

Scraping Data for Earthquake API

In this step, we’ll inspect more deeply how to extract data from a remote source. We will use “command” classes to scrape data in the background. If you do not know about artisan commands in Laravel you should check this URL.

I will create a command named ScrapeEarthquakes. I need to run this command to do it.

php artisan make:command ScrapeEarthquakes
----------------------------------------------------
Job created successfully.

We can find this file under earthquake-api/app/console/Commands/ScrapeEarthquakes.php. I will code my scraper code in this job file. Let’s code it.

Here is the content of the ScrapeEarthquakes.php file

With this command file, we can run this process with a command line or cronjob. We can run this command with the command below

php artisan scrape:earthquakes

As you can see the results are in the image below. These rows came from our scraper. We need to create a cronjob to repeat this command. In cPanel under cronjobs you can use this command:

php /home/[username]/public_html/artisan scrape:earthquakes
Scraping results from the database

Creating Earthquake API

Create a Controller to Serve Data

We need a controller file that serves our earthquake data to users. You need to run this command to create a controller file.

php artisan make:controller EarthquakeController
----------------------------------------------------
Controller created successfully.

You can find this file under earthquake-api/app/Http/Controllers/EarthquakeController.php. Here is my code that I wrote for the controller.

Create Route for The API Interface

This is the last step for this tutorial. We need to create a routing rule to serve data from URL. Here is my API route rule file. You can find this file under earthquake-api/routes/api.php

This step-by-step tutorial is finished. You can test your API service with a test server that you can set up by running this code to set up a web server.

php artisan serve
----------------------------------------------------
PHP 8.0.3 Development Server (http://127.0.0.1:8003) started

I hope you enjoy this tutorial.

If you got any questions you can ask me in the comments section below.

See you in next post!