Berikut adalah cara membuat API menggunakan PHP pada Mysql :
1. Membuat RestAPI
1.1. Buat Database dan Table : Membuat Database di XAMPP
Database : dtMysql
Table :Karyawan
1.2. Buat file koneksi.php
<?php
$hostname = "localhost";
$database = "dtMysql";
$username = "root";
$password = "";
$connect = mysqli_connect($hostname, $username, $password, $database);
// script cek koneksi
if (!$connect) {
die("Koneksi Tidak Berhasil: " . mysqli_connect_error());
}
1.3. Buat file restapi.php
<?php
require_once "koneksi.php";
if(function_exists($_GET['function'])) {
$_GET['function']();
}
function getkaryawan()
{
global $connect;
$query = $connect->query("Select * From karyawan");
while($row=mysqli_fetch_object($query))
{
$data[] =$row;
}
$response=array(
'status' => 1,
'message' =>'Success',
'data' => $data
);
header('Content-Type: application/json');
echo json_encode($response);
}
function getkaryawanid()
{
global $connect;
if (!empty($_GET["id"])) {
$id = $_GET["id"];
}
$query ="Select * From karyawan Where id= $id";
$result = $connect->query($query);
while($row = mysqli_fetch_object($result))
{
$data[] = $row;
}
if($data)
{
$response = array(
'status' => 1,
'message' =>'Success',
'data' => $data
);
}else {
$response=array(
'status' => 0,
'message' =>'No Data Found'
);
}
header('Content-Type: application/json');
echo json_encode($response);
}
function insertkaryawan()
{
global $connect;
$check = array('id' => '', 'nama' => '', 'jenis_kelamin' => '', 'alamat' => '');
$check_match = count(array_intersect_key($_POST, $check));
if($check_match == count($check)){
$result = mysqli_query($connect, "Insert Into karyawan Set
id = '$_POST[id]',
nama = '$_POST[nama]',
jenis_kelamin = '$_POST[jenis_kelamin]',
alamat = '$_POST[alamat]'");
if($result)
{
$response=array(
'status' => 1,
'message' =>'Insert Success'
);
}
else
{
$response=array(
'status' => 0,
'message' =>'Insert Failed.'
);
}
}else{
$response=array(
'status' => 0,
'message' =>'Wrong Parameter'
);
}
header('Content-Type: application/json');
echo json_encode($response);
}
function updatekaryawan()
{
global $connect;
if (!empty($_GET["id"])) {
$id = $_GET["id"];
}
$check = array('nama' => '', 'jenis_kelamin' => '', 'alamat' => '');
$check_match = count(array_intersect_key($_POST, $check));
if($check_match == count($check)){
$result = mysqli_query($connect, "Update karyawan Set
nama = '$_POST[nama]',
jenis_kelamin = '$_POST[jenis_kelamin]',
alamat = '$_POST[alamat]' Where id = $id");
if($result)
{
$response=array(
'status' => 1,
'message' =>'Update Success'
);
}
else
{
$response=array(
'status' => 0,
'message' =>'Update Failed'
);
}
}else{
$response=array(
'status' => 0,
'message' =>'Wrong Parameter',
'data'=> $id
);
}
header('Content-Type: application/json');
echo json_encode($response);
}
function deletekaryawan()
{
global $connect;
$id = $_GET['id'];
$query = "Delete From karyawan Where id=".$id;
if(mysqli_query($connect, $query))
{
$response=array(
'status' => 1,
'message' =>'Delete Success'
);
}
else
{
$response=array(
'status' => 0,
'message' =>'Delete Fail.'
);
}
header('Content-Type: application/json');
echo json_encode($response);
}
?>
2. Pemanggilan Data REST Server Menggunakan Postman
Gunakan metode GET dan POST. Untuk dapat mengakses file server. jadikan postman sebagai client yang akan mengakses data server.
2.1. Insert menggunakan POST
http://localhost/php/restapi.php?function=insertkaryawan
2.2. Rest API GET
Untuk memanggil semua data karyawan gunakan
http://localhost/php/restapi.php?function=getkaryawan
2.3. Get Karyawan By Id
http://localhost/php/restapi.php?function=getkaryawanid&id=1
2.4. Update Data
http://localhost/php/restapi.php?function=updatekaryawan&id=1
2.5. Delete Data
http://localhost/php/restapi.php?function=deletekaryawan&id=6