Module 1 – Introduction to API and HTTP
Exercise
Exercise: first API call
Exercise: first API call
Hands-on exercise: Exercise: first API call. Build a small PHP 8 REST API and verify the HTTP layer before adding authentication and deployment. U REST API Masterclass kursu primenjujete PHP 8, JSON odgovore, jasne status kodove i produkcijsku strukturu projekta pre nego što API pustite klijentima ili mobilnim aplikacijama.
Task
- Create api-lab/ with public/index.php, src/Router.php, and config/.env sample.
- Run PHP built-in server: php -S localhost:8080 -t api-lab/public.
- Implement minimal router and one GET endpoint returning JSON with status 200.
- Add POST endpoint with input validation and 201 Created response.
- Test both endpoints with curl and save requests in Postman collection api-lab.json.
- Simulate error (invalid JSON) and verify API returns 400 with clear message.
- Add log file storage/requests.log with method, path, status, and timestamp.
- Optional: connect endpoint to SQLite users table for small CRUD example.
Practical example
Test API call
curl -i http://localhost:8080/api/usersGoal: Successfully implement Exercise: first API call with correct JSON responses, status codes, and documented curl examples ready for team code review. U REST API Masterclass kursu primenjujete PHP 8, JSON odgovore, jasne status kodove i produkcijsku strukturu projekta pre nego što API pustite klijentima ili mobilnim aplikacijama.
