Skip to content

About

PHP API using PDO, a custom router, and MySQL

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

phptestapi

A small PHP API using PDO, a custom router, and MySQL. PHPUnit tests use an in-memory SQLite database and do not need a running MySQL server.

Requirements

  • PHP 8.2 or newer with pdo_mysql, pdo_sqlite, mbstring, and openssl
  • Composer 2
  • MySQL for running the API

Setup

From this directory, install dependencies and create your local environment file:

composer install
Copy-Item .env.example .env

Create the database named in DB_NAME in .env, then set DB_HOST, DB_USERNAME, and DB_PASSWORD for your MySQL installation. Replace JWT_SECRET with a long, random value before using the API. Set STRIPE_SECRET_KEY to enable Stripe payments.

Run the migrations:

php cli/run_migrations.php migrate

Run the API

Start PHP's built-in server with the router script so API paths are forwarded to the application:

php -S 127.0.0.1:8000 -t public public/router.php

The API is available at http://127.0.0.1:8000. Existing endpoints are:

Method Path Purpose
POST /register Create a user
POST /login Authenticate a user
GET /users List users
GET /users/{id} Get a user
POST /users Create a user
PUT /users/{id} Update a user
DELETE /users/{id} Delete a user
POST /payment Process a Stripe payment
POST /webhook Receive a Stripe event

The user endpoints require an Authorization: Bearer <token> header from /login. Registration and login are public. Requests that create or update users accept JSON. For example:

{
  "fullname": "Ada Lovelace",
  "email": "ada@example.com",
  "password": "use-a-strong-password"
}

Run the tests

Install dependencies first, then run the test suite from this directory:

composer test

The tests use SQLite :memory: and require PHP's pdo_sqlite extension. They do not read .env or modify your MySQL database.

To run PHPUnit directly:

php vendor/phpunit/phpunit/phpunit --configuration phpunit.xml

Migrations

php cli/run_migrations.php migrate
php cli/run_migrations.php rollback

About

PHP API using PDO, a custom router, and MySQL

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages