- This is the simple PHP Restful API which is implemented without using frameworks.
- In this demo project, I built login/logout function and implemented an endpoint (/users) to consume request from client so that we can select and update user's data (such as name, address, phone ...)
- I try to implement this demo follow single-page's style for login/logout so that we nearly unchange the URI.
- I also try to implement some function to secure the login process and API calling such as: prevent cookies thief, using pair of email address and access key to enable API call.
There are 2 main functions in this demo:
- User Login / Logout to view user's information and API Key (API Key is an base64 encoded string)
- 2 endpoints to call API (/users and /users/:id)
- Ensure mode_rewrite and PDO are support by your web server.
- Import db.sql (include in source) to your.
- Change connection detail in helper.php file.
- Copy source files to your web root directory.
- View database record to get username and password to login.
- When logged in you will see API Key in User's information.
- Using "email:key" as user parameter in your request header to call API.
- http://example.com/api/users to get users list. Only GET is supported.
- http://example.com/api/users/:id to get and update user. GET and PUT are supported
Each request must be authenticated with user’s e-mail and API key.
Request Example:
- GET: curl --user [email protected]:APIkey -X GET 'http://example.com/api/users/1'
- PUT: curl --user [email protected]:APIkey --header 'Content-Type: application/json' -d '{"name":"Neymar","address":"PSG FC, France","telephone":"0109090909"}' -X PUT 'http://example.com/api/users/1’