Make Web Service in PHP for Android Application

Web services is one type of medium for accessing the data in different devices, here we take example like make service for the getting the list of the doctors, using this web service android developer can display doctor on android application.

1. Make table in my SQL for storing the doctor data,

  1. CREATE TABLE IF NOT EXISTS `tbl_doctormaster` (  
  2.   `dm_Id` int(11) NOT NULL AUTO_INCREMENT,  
  3.   `dm_Name` varchar(50) NOT NULL,  
  4.   `dm_Address` varchar(1000) NOT NULL,  
  5.   `dm_MobileNo` varchar(25) NOT NULL,  
  6.   `dm_EmailAddress` varchar(50) NOT NULL,  
  7.   `dm_Password` varchar(100) NOT NULL,    
  8.   `dm_ActiveStatus` int(11) NOT NULL DEFAULT '1',  
  9.   `dm_CreatedDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  
  10.   `dm_UpdatedDate` timestamp NOT NULL,  
  11.   PRIMARY KEY (`dm_Id`)  
  12. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;  
2. Now insert data in datable,
  1. INSERTINTO `tbl_doctormaster`(`dm_Id`, `dm_Name`, `dm_Address`, `dm_MobileNo`, `dm_EmailAddress`, `dm_Password`, `dm_ActiveStatus`, `dm_CreatedDate`, `dm_UpdatedDate`)VALUES  
  2. (1,'kapil111','klklkl','89898898','[email protected]','sdfdsf', 2,'2016-01-11 17:09:51','2016-01-11 17:09:51'),  
  3. (2,'Nirav Daraniya','sdfsdfsdfdsf','899898989','[email protected]','sdfsdfsdfds', 1,'2016-01-11 17:10:33','2016-01-11 17:10:33'),  
  4. (3,'aaaaaa11','asss11','89898998911','[email protected]','989988', 2,'2016-01-11 17:14:37','2016-01-11 17:14:37'),  
  5. (4,'l','l','7777777778','[email protected]','7897987', 2,'2016-01-13 19:32:51','2016-01-13 19:32:51');  
3. Now make One PHP file for service give its name “service_getdoctor.php”,
  1. <?php  
  2. $servername = "localhost";  
  3. $username = "root or Username";  
  4. $password = "Your My SQL password";  
  5. $conn = new mysqli($servername$username$password);  
  6. mysqli_select_db($conn"YourDatabaseName");  
  7. $getData = "SELECT `dm_Id`, `dm_Name`, `dm_Address`, `dm_MobileNo`, `dm_EmailAddress`, `dm_ActiveStatus`, `dm_CreatedDate`, `dm_UpdatedDate` FROM `tbl_doctormaster`";  
  8. $result = mysqli_query($conn$getData);  
  9. $cnt = 0;  
  10.   
  11. while ($r = mysqli_fetch_row($result))  
  12.     {  
  13.     $cnt = 1;  
  14.     $msg[] = array(  
  15.         "dm_Id" => $r[0],  
  16.         "dm_Name" => $r[1],  
  17.         "dm_Address" => $r[2],  
  18.         "dm_MobileNo" => $r[3],  
  19.         "dm_EmailAddress" => $r[4],  
  20.         "dm_ActiveStatus" => $r[5],  
  21.         "dm_CreatedDate" => $r[6]  
  22.     );  
  23.     }  
  24.   
  25. $json = $msg;  
  26. header('content-type: application/json');  
  27.   
  28. if ($cnt == 0)  
  29.     {  
  30.     $resp["status"] = "-1";  
  31.     $resp["message"] = "Data Not Found";  
  32.     }  
  33.   else  
  34.     {  
  35.     $resp["status"] = "1";  
  36.     $resp["data"] = $msg;  
  37.     }  
  38.   
  39. $response["response"] = $resp;  
  40. echojson_encode($response);  
  41. @mysqli_close($conn);  
  42. ?>  
4. Now run this php file(service it give below output),

{"response":{"status":"1","data":[{"dm_Id":"1","dm_Name":"kapil111","dm_Address":"klklkl","dm_MobileNo":"89898898","dm_EmailAddress":"[email protected]","dm_ActiveStatus":"2","dm_CreatedDate":"2016-01-11 22:39:51"},{"dm_Id":"2","dm_Name":"Nirav Daraniya","dm_Address":"sdfsdfsdfdsf","dm_MobileNo":"899898989","dm_EmailAddress":"[email protected]","dm_ActiveStatus":"1","dm_CreatedDate":"2016-01-11 22:40:33"},{"dm_Id":"3","dm_Name":"aaaaaa11","dm_Address":"asss11","dm_MobileNo":"89898998911","dm_EmailAddress":"[email protected]","dm_ActiveStatus":"2","dm_CreatedDate":"2016-01-11 22:44:37"},{"dm_Id":"4","dm_Name":"l","dm_Address":"l","dm_MobileNo":"7777777778","dm_EmailAddress":"[email protected]","dm_ActiveStatus":"2","dm_CreatedDate":"2016-01-14 01:02:51"}]}}