├── README.md └── server ├── apis ├── login.php ├── signup.php └── user.php ├── common ├── database.php ├── encipher.php ├── header.php ├── include.php └── response.php └── index.php /README.md: -------------------------------------------------------------------------------- 1 | # REST APIs using PHP. 2 | Repository of creating REST APIs using PHP & MySql. 3 | ## Documentation 4 | Please read this article for better understanding of this project - [RESTful APIs using PHP and Mysql](https://kumarmanishc.medium.com/restful-apis-using-php-and-mysql-dc7e6f96db8e) 5 | ## Installing 6 | All you need to do is copy server folder of repository in your server( Apache, etc. ) 7 | * Create database of your choice with table user(id, name, email, password, contact). 8 | * For Linux you can copy that server folder to /var/www/html. 9 | * For windows copy to www folder of WAMP/XAAMP. 10 | -------------------------------------------------------------------------------- /server/apis/login.php: -------------------------------------------------------------------------------- 1 | email){ 6 | sendResponse(400, [] , 'Email Required !'); 7 | }else if(!$user->password){ 8 | sendResponse(400, [] , 'Password Required !'); 9 | }else{ 10 | $conn=getConnection(); 11 | if($conn==null){ 12 | sendResponse(500,$conn,'Server Connection Error !'); 13 | }else{ 14 | $password=doEncrypt($user->password); 15 | $sql = "SELECT id, name, email, contact FROM user WHERE email='"; 16 | $sql.=$user->email."' AND password = '".$password."'"; 17 | $result = $conn->query($sql); 18 | if ($result->num_rows > 0) { 19 | $users=array(); 20 | while($row = $result->fetch_assoc()) { 21 | $user=array( 22 | "id" => $row["id"], 23 | "name" => $row["name"], 24 | "email" => $row["email"], 25 | "contact" => $row["contact"], 26 | ); 27 | array_push($users,$user); 28 | } 29 | sendResponse(200,$users,'User Details'); 30 | } else { 31 | sendResponse(404,[],'User not available'); 32 | } 33 | $conn->close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /server/apis/signup.php: -------------------------------------------------------------------------------- 1 | name){ 7 | sendResponse(400, [] , 'Name Required !'); 8 | }else if(!$user->email){ 9 | sendResponse(400, [] , 'Email Required !'); 10 | }else if(!$user->password){ 11 | sendResponse(400, [] , 'Password Required !'); 12 | }else if(!$user->contact){ 13 | sendResponse(400, [] , 'Contact Required !'); 14 | }else{ 15 | $password = doEncrypt($user->password); 16 | $conn=getConnection(); 17 | if($conn==null){ 18 | sendResponse(500, $conn, 'Server Connection Error !'); 19 | }else{ 20 | $sql="INSERT INTO user(name, email, password, contact)"; 21 | $sql .= "VALUES ('".$user->name."','".$user->email."','"; 22 | $sql .= $password."','".$user->contact."')"; 23 | 24 | $result = $conn->query($sql); 25 | if ($result) { 26 | sendResponse(200, $result , 'User Registration Successful.'); 27 | } else { 28 | sendResponse(404, [] ,'User not Registered'); 29 | } 30 | $conn->close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /server/apis/user.php: -------------------------------------------------------------------------------- 1 | query($sql); 10 | 11 | if ($result->num_rows > 0) { 12 | $users=array(); 13 | while($row = $result->fetch_assoc()) { 14 | $user=array( 15 | "id" => $row["id"], 16 | "name" => $row["name"], 17 | "email" => $row["email"], 18 | "contact" => $row["contact"], 19 | ); 20 | array_push($users,$user); 21 | } 22 | sendResponse(200,$users,'User List'); 23 | } else { 24 | sendResponse(404,[],'User not available'); 25 | } 26 | $conn->close(); 27 | } 28 | ?> 29 | -------------------------------------------------------------------------------- /server/common/database.php: -------------------------------------------------------------------------------- 1 | connect_error) { 10 | $conn= null; 11 | } 12 | return $conn; 13 | } 14 | -------------------------------------------------------------------------------- /server/common/encipher.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /server/common/response.php: -------------------------------------------------------------------------------- 1 | $resp_code,'message'=>$message,'data'=>$data)); 7 | } -------------------------------------------------------------------------------- /server/index.php: -------------------------------------------------------------------------------- 1 | --------------------------------------------------------------------------------