DEV Community

Cover image for OpenCode REST API Application with PHP and SQLITE. Articulate By: Md Shoeb Lincoln
MD.Shoeb Lincoln
MD.Shoeb Lincoln

Posted on

OpenCode REST API Application with PHP and SQLITE. Articulate By: Md Shoeb Lincoln


Uploading image# Mini REST API — PHP + SQLite, all-JSON

rest-api/
├── public/index.php   # front controller + router
├── public/router.php  # php -S router
├── src/Database.php   # SQLite (db/app.db auto-created)
├── src/Response.php   # JSON helpers
├── src/TaskController.php  # CRUD
├── openapi.json       # complete API spec
Enter fullscreen mode Exit fullscreen mode

Run

php -S localhost:8130 -t public public/router.php
Enter fullscreen mode Exit fullscreen mode

Try it

curl http://localhost:8130/api
curl -X POST http://localhost:8130/api/tasks \
  -H 'Content-Type: application/json' \
  -d '{"title":"Write docs","priority":"high"}'
curl http://localhost:8130/api/tasks
curl http://localhost:8130/api/tasks/1
curl -X PATCH http://localhost:8130/api/tasks/1 \
  -H 'Content-Type: application/json' -d '{"done":true}'
curl -X DELETE http://localhost:8130/api/tasks/1
Enter fullscreen mode Exit fullscreen mode

List supports ?done=0|1&q=...&page=&per_page=. All responses are
{ok, data} or {ok:false, error} with proper status codes
(200/201/404/405/422).

Top comments (0)