DEV Community

MD.Shoeb Lincoln
MD.Shoeb Lincoln

Posted on AI-assisted

OpenCode REST API Application with PHP and SQLITE

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

Enter fullscreen mode Exit fullscreen mode

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


## Run

Enter fullscreen mode Exit fullscreen mode


bash
php -S localhost:8130 -t public public/router.php


## Try it

Enter fullscreen mode Exit fullscreen mode


bash
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


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).

Enter fullscreen mode Exit fullscreen mode

Top comments (0)