An open API service indexing awesome lists of open source software.

https://github.com/erykmika/trim-it

URL Shortener REST API written in PHP 8
https://github.com/erykmika/trim-it

api mysql php php8 rest-api

Last synced: 5 months ago
JSON representation

URL Shortener REST API written in PHP 8

Awesome Lists containing this project

README

          

# trim-it

A simple REST API written in PHP 8 without using any particular frameworks. It enables a user to shorten ("trim") a URL and generate its hash that can be used to retrieve the original URL. The application code was structured in such a way it follows the MVC architecture pattern. **The project is still in development**.

## Tech stack

| Type | Technology used |
| ---------------------------- |----------------------------------------------------------------------|
| Core scripting language | PHP 8 |
| Database (dev & test) | MySQL |
| Unit testing | PHPUnit |
| Dependency management | composer |

## Project setup

PHP >= 8.3 is recommended. An instance of active MySQL database with permissions to create/manage databases/users is needed.

### Clone the repository

```sh
git clone https://github.com/erykmika/trim-it.git
```

### Install dependencies (composer)

```sh
cd ./trim-it/
composer install
```
This assumes the global installation of composer.

### Configure the database

Modify **include/db_config.php** and, optionally, **tests/test_db_config.php** to provide database credentials for a development and a testing database respectively. Run the **create_schema.sql** script for both databases to create a proper schema for the application:

```sh
mysql -u -p -d < ./sql/create_schema.sql
```

### Run the application

You can use the Apache HTTP Server to handle HTTP requests. You will have to expose the **public** directory in the configuration. An **.htaccess** file specifying rewriting rules is provided. For the sake of simplicity, you can also use the in-built PHP web development server:

```sh
cd ./public/
php -S localhost:8080
```

Now the application will respond to the API requests on the specified addresss.

## Endpoints

The application handles and serves JSON-encoded requests/responses.

| Method | URL | Description |
|-----------|---------------|-------------------|
| GET | /url/:hash | Get full URL |
| POST | /hash | Generate URL hash |

### POST JSON body structure

```json
{
"url": ""
}

```

### Response JSON body structure
```json
{
"status": "",
"": ""
}
```

## Unit testing

To launch tests, run the vendor-provided PHPUnit executable.

```sh
./vendor/bin/phpunit --testdox
```

The **--testdox** flag provides more user-friendly output of the tests.