Contoh CRUD Web client SOAP WSDL menggunakan nusoap

Sebelumnya sebagai catatan, aplikasi ini dibuat dan dijalankan dengan lancar dalam operating system linux (UBUNTU, Linuxmint, dan Elementary OS). Bagi teman-teman mungkin akan mendapati error untuk function readall (Return Array). Bagi temen2 yang bisa ngasih solusi, bisa ditulis di kolom komen, nanti akan saya update secepatnya. 😀

Aplikasi sudah jadi bisa didownload disini

Setelah membahas membuat web service menggunakan nusoap, sekarang kita akan coba membuat web client untuk mengakses web service yang kita buat. Kita akan menggunakan codeigniter untuk membuat web client.

Download semua terlebih dahulu yang dibutuhkan.

 

Exstract codeigniter yang telah didownload, kemudian buat folder “public” di dalam folder yang telah kita exstract tadi. Kemudian copas isi folder “dist” hasil download twitter bootstrap tadi. Setelah selesai setting2 folder, kita akan konsen membuat “controller” dan “view” untuk web service.

Edit file config.php pada bagian base_url :

$config['base_url'] = 'http://localhost/clientperpus/';

Edit file welcome.php menjadi :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->library('nusoap_library');
        $this->load->helper('url');
    }
    public function index()
    {
        $this->load->view('welcome_message');
    }
    //crud kategori buku
    public function kategori(){
        $client = new nusoap_client("http://127.0.0.1/wsperpus/wskategori.php");//alamat web service

        $error = $client->getError();//respon web service error
        if ($error) {
            echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
        }


        if ($client->fault) {//web service client fault
            echo "error";
        }else {
            $error = $client->getError();//web service client error
            if ($error) {
                echo "<h2>Error</h2><pre>" . $error . "</pre>";
            }else {
                $result = $client->call("readall");//respon web service
                $data = array('itemkategoribuku'=>$result);
                $this->load->view('kategoriform', $data, FALSE);
            }
        }
    }
    public function createkategori(){
        if (!$this->input->is_ajax_request()) {
            echo show_404();
        }else{
            $client = new nusoap_client("http://127.0.0.1/wsperpus/wskategori.php/");//alamat web service

            $error = $client->getError();//respon web service error
            if ($error) {
                $status = "error";
                $msg = "<h2>Constructor error</h2><pre>" . $error . "</pre>";
            }

            $result = $client->call("create", array("kategori_buku" => $this->input->post('kategori_buku')));//respon web service

            if ($client->fault) {//web service client fault
                $status = "error";
                $msg = $result;
            }else {
                $error = $client->getError();//web service client error
                if ($error) {
                    $status = "error";
                    $msg = "<h2>Error</h2><pre>" . $error . "</pre>";
                }else {
                    if ($result=="sukses") {
                        $status = "success";
                        $msg = "data berhasil disimpan";
                    }else{
                        $status = "error";
                        $msg = "terjadi kesalahan saat menyimpan data, atau data sudah ada";
                    }
                }
            }
            $this->output->set_content_type('application/json')->set_output(json_encode(array('status'=>$status,'msg'=>$msg)));
        }
    }
    public function editkategori(){
        if (!$this->input->is_ajax_request()) {
            show_404();
        }else{
            $client = new nusoap_client("http://127.0.0.1/wsperpus/wskategori.php");//alamat web service

            $error = $client->getError();//respon web service error
            if ($error) {
                $status = 'error';
                $msg = "<h2>Constructor error</h2><pre>" . $error . "</pre>";
                $kategori = null;
            }

            $result = $client->call("readbyid", array("id_kategori_buku" => $this->input->post('id_kategori_buku')));//respon web service

            if ($client->fault) {//web service client fault
                $status = 'error';
                $msg = "Error";
                $kategori = null;
            }else {
                $error = $client->getError();//web service client error
                if ($error) {
                    $status = 'error';
                    $msg = "<h2>Error</h2><pre>" . $error . "</pre>";
                    $kategori = null;
                }else {
                    $status = 'success';
                    $msg = 'data ditemukan';
                    $kategori = $result;
                }
            }

            $this->output->set_content_type('application/json')->set_output(json_encode(array('status'=>$status,'msg'=>$msg,'kategori'=>$kategori)));
        }
    }
    public function updatekategori(){
        if (!$this->input->is_ajax_request()) {
            show_404();
        }else{
            $client = new nusoap_client("http://127.0.0.1/wsperpus/wskategori.php");//alamat web service

            $error = $client->getError();//respon web service error
            if ($error) {
                $status = "error";
                $msg = "<h2>Constructor error</h2><pre>" . $error . "</pre>";
            }

            $result = $client->call("updatebyid", array("id_kategori_buku"=>$this->input->post('id_kategori_buku'),"kategori_buku" => $this->input->post('kategori_buku')));//respon web service

            if ($client->fault) {//web service client fault
                $status = "error";
                $msg = $result;
            }else {
                $error = $client->getError();//web service client error
                if ($error) {
                    $status = "error";
                    $msg = "<h2>Error</h2><pre>" . $error . "</pre>";
                }else {
                    if ($result=="sukses") {
                        $status = "success";
                        $msg = "data berhasil diupdate";
                    }else{
                        $status = "error";
                        $msg = "terjadi kesalahan saat mengupdate data";
                    }
                }
            }
            $this->output->set_content_type('application/json')->set_output(json_encode(array('status'=>$status,'msg'=>$msg)));
        }
    }
    public function removekategori(){
        if (!$this->input->is_ajax_request()) {
            show_404();
        }else{
            $client = new nusoap_client("http://127.0.0.1/wsperpus/wskategori.php");//alamat web service
            $error = $client->getError();//respon web service error
            if ($error) {
                $status = "error";
                $msg = "<h2>Constructor error</h2><pre>" . $error . "</pre>";
            }

            $result = $client->call("deletebyid", array("id_kategori_buku" => $this->input->post('id_kategori_buku')));//respon web service

            if ($client->fault) {//web service client fault
                $status = "error";
                $msg = $result;
            }else {
                $error = $client->getError();//web service client error
                if ($error) {
                    $status = "error";
                    $msg = "<h2>Error</h2><pre>" . $error . "</pre>";
                }
                else {
                    if ($result=="sukses") {
                        $status = "success";
                        $msg = "data berhasil dihapus";
                    }else{
                        $status = "error";
                        $msg = "terjadi kesalahan saat menghapus data";
                    }
                }
            }
            $this->output->set_content_type('application/json')->set_output(json_encode(array('status'=>$status,'msg'=>$msg)));
        }
    }
    //end crud kategori buku
}

Selanjutnya buat file kategoriform.php di folder view :

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="decription" content="Administrator">
    <meta name="author" content="syarif">
    <link rel="shortcut icon" href="">
    <title><?php if(isset($title)){echo $title." || ";} ?>Administrator</title>
    <link rel="stylesheet" type="text/css" href="http://localhost/clientperpus/public/css/bootstrap.css">
    <script src="http://localhost/clientperpus/public/js/jquery-1.11.3.min.js"></script>
    <script src="http://localhost/clientperpus/public/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container">
        <div class="row">
<!--content-->
<div class="col-md-4 col-sm-4">
    <div class="panel panel-primary">
        <div class="panel-heading"><span class="glyphicon glyphicon-list"></span> Form Kategori Buku</div>
        <div class="panel-body">
            <form action="#">
                <div class="form-group">
                    <label for="kategori_buku">Kategori Buku</label>
                    <input type="text" class="form-control" id="kategori_buku" name="kategori_buku">
                    <input type="hidden" id="id_kategori_buku" name="id_kategori_buku">
                </div>
                <div class="form-group">
                    <button class="btn btn-primary" id="simpankategori"><span class="glyphicon glyphicon-save"></span> Simpan</button>
                    <button class="btn btn-primary" id="updatekategori" disabled><span class="glyphicon glyphicon-edit" disabled></span> Update</button>
                    <button class="btn btn-warning" id="resetkategori"><span class="glyphicon glyphicon-refresh"></span> Reset</button>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="col-md-8 col-sm-8">
    <div class="panel panel-primary">
        <div class="panel-heading"><span class="glyphicon glyphicon-th-list"></span> Daftar Kategori Buku</div>
        <div class="panel-body">
            <table class="table">
                <th>No</th>
                <th>Id Kategori</th>
                <th>Kategori Buku</th>
                <th>Action</th>
                <tbody id="daftarkategoribuku">
            <?php if (sizeof($itemkategoribuku)!=null) {
                $no = 1;
                foreach ($itemkategoribuku as $kategori) {
                    echo "<tr>";
                    echo "<td>".$no++."</td>";
                    echo "<td>".$kategori['id_kategori_buku']."</td>";
                    echo "<td>".$kategori['kategori_buku']."</td>";
                    echo "<td>";
                    echo "<button class='btn btn-sm btn-primary' id='editkategori' data-idkategoribuku = '".$kategori['id_kategori_buku']."'><span class='glyphicon glyphicon-edit'></span></button> ";
                    echo "<button class='btn btn-sm btn-danger' id='removekategori' data-idkategoribuku = '".$kategori['id_kategori_buku']."'><span class='glyphicon glyphicon-remove'></span></button>";
                    echo "</td>";
                    echo "</tr>";
                }
                //var_dump($itemkategoribuku);
            } ?>
            </tbody>
            </table>
        </div>
    </div>
</div>
<script>
    $(document).on('click','#simpankategori',simpankategori)
    .on('click','#updatekategori',updatekategori)
    .on('click','#editkategori',editkategori)
    .on('click','#removekategori',removekategori)
    .on('click','#resetkategori',resetkategori);
    function simpankategori(e){
        e.preventDefault();
        var datakategori = {'kategori_buku':$('#kategori_buku').val()};
        console.log(datakategori);
        $.ajax({
            url : '<?php echo site_url("welcome/createkategori") ?>',
            data : datakategori,
            dataType : 'json',
            type : 'POST',
            success : function(data,status){
                if (data.status!='error') {
                    alert(data.msg);
                    resetkategori(e);
                    $('#daftarkategoribuku').load('<?php echo current_url()."/ #daftarkategoribuku > *" ?>');
                }else{
                    alert(data.msg);
                }
            }
        })
    }
    function editkategori(e){
        e.preventDefault();
        var datakategori = {'id_kategori_buku':$(this).data('idkategoribuku')};
        console.log(datakategori);
        $.ajax({
            url : '<?php echo site_url("welcome/editkategori") ?>',
            data : datakategori,
            dataType : 'json',
            type : 'POST',
            success : function(data,status){
                if (data.status!='error') {
                    $.each(data.kategori,function(k,v){
                        $('#id_kategori_buku').val(v['id_kategori_buku']);
                        $('#kategori_buku').val(v['kategori_buku']);
                    })
                    $('#simpankategori').attr('disabled',true);
                    $('#updatekategori').attr('disabled',false);
                    alert(data.msg);
                }else{
                    alert(data.msg);
                }
            }
        })
    }
    function updatekategori(e){
        e.preventDefault();
        var datakategori = {'id_kategori_buku':$('#id_kategori_buku').val(),'kategori_buku':$('#kategori_buku').val()};
        console.log(datakategori);
        $.ajax({
            url : '<?php echo site_url("welcome/updatekategori") ?>',
            data : datakategori,
            dataType : 'json',
            type : 'POST',
            success : function(data,status){
                if (data.status!='error') {
                    alert(data.msg);
                    $('#daftarkategoribuku').load('<?php echo current_url()."/ #daftarkategoribuku > *" ?>');
                    resetkategori(e);
                }else{
                    alert(data.msg);
                }
            }
        })
    }
    function removekategori(e){
        e.preventDefault();
        var datakategori = {'id_kategori_buku':$(this).data('idkategoribuku')};
        console.log(datakategori);
        $.ajax({
            url : '<?php echo site_url("welcome/removekategori") ?>',
            data : datakategori,
            dataType : 'json',
            type : 'POST',
            success : function(data,status){
                if (data.status!='error') {
                    alert(data.msg);
                    $('#daftarkategoribuku').load('<?php echo current_url()."/ #daftarkategoribuku > *" ?>');
                }else{
                    alert(data.msg);
                }
            }
        })
    }
    function resetkategori(e){
        e.preventDefault();
        $('#simpankategori').attr('disabled',false);
        $('#updatekategori').attr('disabled',true);
        $('#id_kategori_buku').val('');
        $('#kategori_buku').val('');
    }
</script>
<!--content-->
        </div>
            <p class="text-center">&copy Copyright <a href="www.fadlur.com">Fadlur</a></p>
    </div>
</body>
</html>

Oh ya jangan lupa copas folder “lib” nusoap hasil download tadi ke folder libraries, dan buat file Nusoap_library.php :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Nusoap_library
{
    function Nusoap_library(){
        require_once('lib/nusoap.php');
    }

}

/* End of file nusoap_library.php */
/* Location: ./application/libraries/nusoap_library.php */

Sekarang kita coba akses dengan alamat “http://localhost/clientperpus/index.php/welcome/kategori”

web client

9 thoughts on “Contoh CRUD Web client SOAP WSDL menggunakan nusoap

  1. yoga Balas

    mas fadlur klo web client soap wsdl pake php native bisa ?
    blm biasa pake framework nih 🙂 mksh

    • fadlur PenulisBalas

      Bisa mas, itu web servicenya kan aku pake php native. cuma yang clientnya pake codeigniter. hehe..

  2. DANY Balas

    A PHP Error was encountered

    Severity: Warning

    Message: Invalid argument supplied for foreach()

    Filename: views/kategoriform.php

    Line Number: 49

    Backtrace:

    File: C:\xampp\htdocs\clientperpus\application\views\kategoriform.php
    Line: 49
    Function: _error_handler

    File: C:\xampp\htdocs\clientperpus\application\controllers\Welcome.php
    Line: 50
    Function: view

    File: C:\xampp\htdocs\clientperpus\index.php
    Line: 292
    Function: require_once

Tinggalkan Balasan

This site uses Akismet to reduce spam. Learn how your comment data is processed.