├── .htaccess ├── README.md ├── db_kampus.sql ├── koneksi.php ├── mahasiswa.php └── method.php /.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On # Turn on the rewriting engine 2 | RewriteRule ^mahasiswa/?$ mahasiswa.php [NC,L] 3 | RewriteRule ^mahasiswa/([0-9]+)/?$ mahasiswa.php?id=$1 [NC,L] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimpleApiPHP 2 | Contoh API PHP Sederhana 3 | 4 | - Import db_kampus.sql 5 | - Ubah koneksi.php sesuaikan dengan host,username,password, dan db anda 6 | -------------------------------------------------------------------------------- /db_kampus.sql: -------------------------------------------------------------------------------- 1 | /* 2 | SQLyog Enterprise v12.4.3 (64 bit) 3 | MySQL - 10.1.25-MariaDB : Database - db_kampus 4 | ********************************************************************* 5 | */ 6 | 7 | /*!40101 SET NAMES utf8 */; 8 | 9 | /*!40101 SET SQL_MODE=''*/; 10 | 11 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 12 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 13 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 14 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 15 | CREATE DATABASE /*!32312 IF NOT EXISTS*/`db_kampus` /*!40100 DEFAULT CHARACTER SET latin1 */; 16 | 17 | USE `db_kampus`; 18 | 19 | /*Table structure for table `tbl_mahasiswa` */ 20 | 21 | DROP TABLE IF EXISTS `tbl_mahasiswa`; 22 | 23 | CREATE TABLE `tbl_mahasiswa` ( 24 | `id` int(11) NOT NULL AUTO_INCREMENT, 25 | `nim` char(8) DEFAULT NULL, 26 | `nama` varchar(200) DEFAULT NULL, 27 | `jk` char(1) DEFAULT NULL, 28 | `alamat` text, 29 | `jurusan` varchar(200) DEFAULT NULL, 30 | PRIMARY KEY (`id`) 31 | ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; 32 | 33 | /*Data for the table `tbl_mahasiswa` */ 34 | 35 | insert into `tbl_mahasiswa`(`id`,`nim`,`nama`,`jk`,`alamat`,`jurusan`) values 36 | (1,'15119999','muhammad al fatih','L','Jl. P. Puger 2 No 19','Informatika'), 37 | (2,'16119212','Fatimah','P','Jl. Ringin Raya','Sistem Informasi'); 38 | 39 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 40 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 41 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 42 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; 43 | -------------------------------------------------------------------------------- /koneksi.php: -------------------------------------------------------------------------------- 1 | 12 | -------------------------------------------------------------------------------- /mahasiswa.php: -------------------------------------------------------------------------------- 1 | get_mhs($id); 11 | } 12 | else 13 | { 14 | $mhs->get_mhss(); 15 | } 16 | break; 17 | case 'POST': 18 | if(!empty($_GET["id"])) 19 | { 20 | $id=intval($_GET["id"]); 21 | $mhs->update_mhs($id); 22 | } 23 | else 24 | { 25 | $mhs->insert_mhs(); 26 | } 27 | break; 28 | case 'DELETE': 29 | $id=intval($_GET["id"]); 30 | $mhs->delete_mhs($id); 31 | break; 32 | default: 33 | // Invalid Request Method 34 | header("HTTP/1.0 405 Method Not Allowed"); 35 | break; 36 | break; 37 | } 38 | 39 | 40 | 41 | 42 | ?> -------------------------------------------------------------------------------- /method.php: -------------------------------------------------------------------------------- 1 | query($query); 12 | while($row=mysqli_fetch_object($result)) 13 | { 14 | $data[]=$row; 15 | } 16 | $response=array( 17 | 'status' => 1, 18 | 'message' =>'Get List Mahasiswa Successfully.', 19 | 'data' => $data 20 | ); 21 | header('Content-Type: application/json'); 22 | echo json_encode($response); 23 | } 24 | 25 | public function get_mhs($id=0) 26 | { 27 | global $mysqli; 28 | $query="SELECT * FROM tbl_mahasiswa"; 29 | if($id != 0) 30 | { 31 | $query.=" WHERE id=".$id." LIMIT 1"; 32 | } 33 | $data=array(); 34 | $result=$mysqli->query($query); 35 | while($row=mysqli_fetch_object($result)) 36 | { 37 | $data[]=$row; 38 | } 39 | $response=array( 40 | 'status' => 1, 41 | 'message' =>'Get Mahasiswa Successfully.', 42 | 'data' => $data 43 | ); 44 | header('Content-Type: application/json'); 45 | echo json_encode($response); 46 | 47 | } 48 | 49 | public function insert_mhs() 50 | { 51 | global $mysqli; 52 | $arrcheckpost = array('nim' => '', 'nama' => '', 'jk' => '', 'alamat' => '', 'jurusan' => ''); 53 | $hitung = count(array_intersect_key($_POST, $arrcheckpost)); 54 | if($hitung == count($arrcheckpost)){ 55 | 56 | $result = mysqli_query($mysqli, "INSERT INTO tbl_mahasiswa SET 57 | nim = '$_POST[nim]', 58 | nama = '$_POST[nama]', 59 | jk = '$_POST[jk]', 60 | alamat = '$_POST[alamat]', 61 | jurusan = '$_POST[jurusan]'"); 62 | 63 | if($result) 64 | { 65 | $response=array( 66 | 'status' => 1, 67 | 'message' =>'Mahasiswa Added Successfully.' 68 | ); 69 | } 70 | else 71 | { 72 | $response=array( 73 | 'status' => 0, 74 | 'message' =>'Mahasiswa Addition Failed.' 75 | ); 76 | } 77 | }else{ 78 | $response=array( 79 | 'status' => 0, 80 | 'message' =>'Parameter Do Not Match' 81 | ); 82 | } 83 | header('Content-Type: application/json'); 84 | echo json_encode($response); 85 | } 86 | 87 | function update_mhs($id) 88 | { 89 | global $mysqli; 90 | $arrcheckpost = array('nim' => '', 'nama' => '', 'jk' => '', 'alamat' => '', 'jurusan' => ''); 91 | $hitung = count(array_intersect_key($_POST, $arrcheckpost)); 92 | if($hitung == count($arrcheckpost)){ 93 | 94 | $result = mysqli_query($mysqli, "UPDATE tbl_mahasiswa SET 95 | nim = '$_POST[nim]', 96 | nama = '$_POST[nama]', 97 | jk = '$_POST[jk]', 98 | alamat = '$_POST[alamat]', 99 | jurusan = '$_POST[jurusan]' 100 | WHERE id='$id'"); 101 | 102 | if($result) 103 | { 104 | $response=array( 105 | 'status' => 1, 106 | 'message' =>'Mahasiswa Updated Successfully.' 107 | ); 108 | } 109 | else 110 | { 111 | $response=array( 112 | 'status' => 0, 113 | 'message' =>'Mahasiswa Updation Failed.' 114 | ); 115 | } 116 | }else{ 117 | $response=array( 118 | 'status' => 0, 119 | 'message' =>'Parameter Do Not Match' 120 | ); 121 | } 122 | header('Content-Type: application/json'); 123 | echo json_encode($response); 124 | } 125 | 126 | function delete_mhs($id) 127 | { 128 | global $mysqli; 129 | $query="DELETE FROM tbl_mahasiswa WHERE id=".$id; 130 | if(mysqli_query($mysqli, $query)) 131 | { 132 | $response=array( 133 | 'status' => 1, 134 | 'message' =>'Mahasiswa Deleted Successfully.' 135 | ); 136 | } 137 | else 138 | { 139 | $response=array( 140 | 'status' => 0, 141 | 'message' =>'Mahasiswa Deletion Failed.' 142 | ); 143 | } 144 | header('Content-Type: application/json'); 145 | echo json_encode($response); 146 | } 147 | } 148 | 149 | ?> --------------------------------------------------------------------------------