API URL : http://domain_name/api.php
Input param :
1 |
{"id":"1","name":"John Smith","address":"New York city","country":"USA","dob":"01011985","username":"john@gmail.com","password":"warRqJXcrICXjQA4dfgsdffgnlnla"} |
API Call Request :
1 |
http://domain_name/api.php?action=add_user&data={"id":"1","name":"John Smith","address":"New York city","country":"USA","DOB":"01011985","username":"john@gmail.com","password":"warRqJXcrICXjQA4dfgsdffgnlnla"} |
Successfull Output :
1 2 3 4 5 6 7 |
{ "error": null, "result": { "status": "Sucess", "id": 123 } } |
Error Output :
1 2 3 4 5 6 7 8 |
{ "error":{ "msg":"User already exist in database. id = 123" }, "result":{ "status":"Failed" } } |
API Script (api.php) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
<?php require_once("Rest.inc.php"); require_once("database_configuration.php"); class API extends REST{ public function __construct() { parent::__construct(); // Init parent contructor } /* * Public method for access api. * This method dynmically call the method based on the query string * */ public function processApi() { $func = strtolower(trim(str_replace("/","",$_REQUEST['action']))); if ((int)method_exists($this,$func) > 0) { $this->$func(); } else { $this->response('',404); // If the method not exist with in this class, response would be "Page not found". } } /* * Insert new User into Database system * method : GET * data : json data * Output : json data */ private function add_user() { // You can even make more secure your webservice by resticting user request as per request type /*if ($this->get_request_method() != "POST") { $arr_res = array(); $arr_res['error'] = array("msg" => "Invalid request method"); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 406); }*/ $data = $this->convert_json_to_array($this->_request['data']); $id = $data['id']; $name = $data['name']; $address = $data['address']; $country = $data['country']; $dob = $data['dob']; $username = $data['username']; $password = $data['password']; // Input validations if (empty($id)) { $arr_res=array(); $arr_res['error'] = array("msg" => "User id not found"); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 400); } if (empty($name)) { $arr_res=array(); $arr_res['error'] = array("msg" => "User Name value not found"); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 400); } if (empty($address)) { $arr_res=array(); $arr_res['error'] = array("msg" => "Address value not found"); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 400); } if (empty($country)) { $arr_res=array(); $arr_res['error'] = array("msg" => "Country value not found"); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 400); } if (empty($dob)) { $arr_res=array(); $arr_res['error'] = array("msg" => "DOB value not found"); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 400); } if (empty($username)) { $arr_res=array(); $arr_res['error'] = array("msg" => "'username' not found"); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 400); } if (empty($password)) { $arr_res=array(); $arr_res['error'] = array("msg" => "'password' not found"); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 400); } if (!empty($id) && !empty($name) && !empty($address) && !empty($country) && !empty($dob) && !empty($username) && !empty($password)) { $query = "SELECT id FROM user WHERE username = '".$username."'"; $result = mysql_query($query); if (mysql_num_rows($result)>0) { $user_data = mysql_fetch_array($result) ; $user_id = $user_data["id"]; $arr_res = array(); $arr_res['error'] = array("msg" => "User already exist in database. id = ".$user_id); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 200); } $insert_query = "INSERT INTO user (id, name, address, country, dob, username, password) values ('".$id."','".$name."','".$address."','".$country."','".$dob."','".$username."','".$password."')"; mysql_query($insert_query); $user_id = mysql_insert_id(); $str_array = array('status' => "Success" ,'id' => $user_id); $arr_res = array(); $arr_res['error'] = $error; $arr_res['result'] = $str_array; $this->response($this->json($arr_res), 200); } // If invalid inputs "Bad Request" status message and reason $error = array("msg" => "Invalid input parameter"); $arr_res = array(); $arr_res['error'] = $error; $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 400); } /* * Delete User from database * method : POST * data : json data * Output : json data */ private function delete_user() { /*if ($this->get_request_method() != "POST") { $arr_res = array(); $arr_res['error'] = array("msg" => "Invalid request method"); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 406); }*/ $data = $this->convert_json_to_array($this->_request['data']); $id = $data['id']; // Input validations if (empty($id)) { $arr_res=array(); $arr_res['error'] = array("msg" => "User id not found"); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 400); } else { $select_query = "SELECT id FROM user WHERE id = '".$id."'"; $result = mysql_query($select_query); if (mysql_num_rows($result) != 1) { $arr_res = array(); $arr_res['error'] = array("msg" => "User not exist in database. id = ".$id); $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 200); } // Delete User from database $delete_query = "DELETE FROM user WHERE id = '".$vm_id."'"; mysql_query($delete_query); $str_array = array('status' => "Success"); $arr_res = array(); $arr_res['error'] = $error; $arr_res['result'] = $str_array; $this->response($this->json($arr_res), 200); } // If invalid inputs "Bad Request" status message and reason $error = array("msg" => "Invalid input parameter"); $arr_res = array(); $arr_res['error'] = $error; $arr_res['result'] = array('status' => "Failed"); $this->response($this->json($arr_res), 400); } /* * Decode JSON into array */ private function convert_json_to_array($json) { return json_decode($json,true); } /* * Encode array into JSON */ private function json($data) { if (is_array($data)) { return json_encode($data); } } } // Initiate Library $api = new API; $api->processApi(); ?> |
database_configuration.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php // Mysql database $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "databasename"; // Create connection $conn = mysql_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } mysql_select_db($dbname); |
You can download all above demos from below link :
Or
Browse source code in github : https://github.com/sagarsdeshmukh/RESTful_Webservice_Demo