If you love JSON, here everything is for you.
Before you begin with this article you should know basic idea about what is API & how it works. In short we can come on a common stage of magic that API is used to communicate with server having a standard format (JSON/XML) of data to be transfer from server to client and vice versa . In this article we will be using standard format of data as JSON, PHP as server language and MySql for storing data. For more about API read this article.
Overview :-
For Example we have a table user(id, name, email, password, contact) with some existing record. Now think what operation we can perform with this table ?
So for sure we will be creating routes for listing user (user.php) and insert new user record (signup). There is a folder server which will hold all codes, files & directory structure of API server using PHP.
For GitHub repository Click here
├─ server/
├─── common/
├────── database.php — file used for connecting to the database.
├────── header.php — file used for configuring response header
├────── response.php — contains methods for sending response. database
├────── encipher.php —contains function for encrypting password.
├────── include.php — file used for including multiple page at once.
├─── apis/
├────── user.php — file that will list user in output as JSON data.
├────── signup.php — this file will take user data and store to database.
├────── login.php — file that will accept email and password and validate user.
Common Folder:-
- database.php :-
2. header.php :-
3. response.php :-
4. encipher.php : –
5. include.php :-
APIS Folder:-
1. User list API :- apis/user.php
1.1. Source code :—
1.2. Response from server : —
Now if all set open browser and type url : http://localhost/server/apis/user.php (for me host is localhost) and then response will as follows.
2. Signup user API :- apis/signup.php
1.1. Source code : —
1.2 Request and Response
Note:- In above tutorial everything I have included for creating API, Now you can enhance this code by using classes , Object and other concept of PHP. For your task you may code for Deleting a user and updating record of a user also login of a user etc..
Share your feedback.
Thank You 🙂