Pages

Monday, April 9, 2018

WEB Service - Arsitektur REST Untuk Menambahkan dan Menampilkan DB

Tugas 5 kuliah Web Service  
 
Nama : M.Imam Catur T.P
NIM  : 16.01.53.0187
 
 
Buatlah program menggunakan arsitektur REST untuk menambah dan menampilkan data mahasiswa 


1. Coding tambahjson.php

<html>
<head>
<title>Rest Web Services</title>
</head>
<body>
<a href="restjson.php">Lihat Datar Mahasiswa <br/></a>
<?php
if (isset ($_POST['nim'])) {
$url = 'http://localhost/webservice/jsonmhsw.php&#8217';
$data="{\"nim\":\"".$_POST['nim']."\",\"nama\":\"".$_POST['nama']."\",\"prodi\":\"".$_POST['progdi']."\"}";
echo "datanya".$data;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
//echo "response ".$response;
curl_close($ch);
}
?>
<form method="POST" action="tambahjson.php">
<table>
<tr>
<td>NIM</td>
<td><input type="text" name="nim" id="nim"></td>
</tr>
<tr>
<td>Nama</td>
<td><input type="text" name="nama" id="nama"></td>
</tr>
<tr>
<td>Progdi</td>
<td><input type="text" name="progdi" id="progdi"></td>
</tr>
<tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="Tambah"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>


2. Coding restjson.php

    <?php
    // Check for the path elements
    // Turn off error reporting
    error_reporting(0);

    // Report runtime errors
    error_reporting(E_ERROR | E_WARNING | E_PARSE);

    // Report all errors
    error_reporting(E_ALL);

    // Same as error_reporting(E_ALL);
    ini_set("error_reporting", E_ALL);

    // Report all errors except E_NOTICE
    error_reporting(E_ALL & ~E_NOTICE);
    $path = $_SERVER[PATH_INFO];
    if ($path != null) {
    $path_params = spliti ("/", $path);
    }
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    if ($path_params[1] == null) {
    $con=mysqli_connect("localhost","root","","akademik");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql="SELECT nim,nama,prodi FROM mahasiswa";
    if ($result=mysqli_query($con,$sql))
    {
    // Fetch one and one row
    $arr = Array();
    while ($row=mysqli_fetch_assoc($result))
    {
    $arr[] = $row;
    }
    echo json_encode($arr);
    // Free result set
    mysqli_free_result($result);
    }
    mysqli_close($con);
    }
    else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    if ($path_params[1] != null) {
    $con=mysqli_connect("localhost","root","","akademik");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql="SELECT nim,nama,prodi FROM mahasiswa where nim = '$path_params[1]'";
    if ($result=mysqli_query($con,$sql))
    {
    // Fetch one and one row
    $arr = Array();
    while ($row=mysqli_fetch_assoc($result))
    {
    $arr[] = $row;
    }
    echo json_encode($arr);
    mysqli_free_result($result);
    }
    mysqli_close($con);
    }
    }
    }
    ?>


3. Coding jsonmhsw.php


    <?php
    // Check for the path elements
    // Turn off error reporting
    error_reporting(0);

    // Report runtime errors
    error_reporting(E_ERROR | E_WARNING | E_PARSE);

    // Report all errors
    error_reporting(E_ALL);

    // Same as error_reporting(E_ALL);
    ini_set("error_reporting", E_ALL);

    // Report all errors except E_NOTICE
    error_reporting(E_ALL & ~E_NOTICE);
    $path = $_SERVER[PATH_INFO];
    if ($path != null) {
    $path_params = spliti ("/", $path);
    }
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    if ($path_params[1] == null) {
    $con=mysqli_connect("localhost","root","","akademik");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql="SELECT nim,nama,prodi FROM mahasiswa";
    if ($result=mysqli_query($con,$sql))
    {
    // Fetch one and one row
    $arr = Array();
    while ($row=mysqli_fetch_assoc($result))
    {
    $arr[] = $row;
    }
    echo json_encode($arr);
    // Free result set
    mysqli_free_result($result);
    }
    mysqli_close($con);
    }
    else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    if ($path_params[1] != null) {
    $con=mysqli_connect("localhost","root","","akademik");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql="SELECT nim,nama,prodi FROM mahasiswa where nim = '$path_params[1]'";
    if ($result=mysqli_query($con,$sql))
    {
    // Fetch one and one row
    $arr = Array();
    while ($row=mysqli_fetch_assoc($result))
    {
    $arr[] = $row;
    }
    echo json_encode($arr);
    mysqli_free_result($result);
    }
    mysqli_close($con);
    }
    }
    }
    ?>



4. Gambar Data dan Hasil
 




 

0 comments:

Post a Comment