Minggu, 17 April 2016

Nusoap Menampilkan seluruh data dalam tabel


saya menyimpan code sisi server dengan nama books3.php
dimpan di htdocs/belajar/webServices/webservicelp2maray/WebService/books3.php

<?php

function fungsiGetListBuku(){
    mysql_connect("localhost","root","");
    mysql_select_db("webservice");

    $result=mysql_query("SELECT * FROM `buku`");

    $index=0;
    while($data=mysql_fetch_array($result)){
        $books[$index]=array(
            "id"=>$data['id'],
            "judul"=>$data['judul'],
            "pengarang"=>$data['pengarang'],
            "penerbit"=>$data['penerbit'],
            "harga"=>$data['harga']
        );
        $index++;
    }

    mysql_close();
    return $books;
}

require("lib/nusoap.php");
$server=new soap_server();
$server->configureWSDL("Books","urn:BooksService");//Books=Name & BooksService=Namespace

//getBookInfo
$server->wsdl->addComplexType(
    "book",
    "complexType",
    "struct",
    "all",
    "",
    array(
        "id"=>array("name"=>"id","type"=>"xsd:string"),
        "judul"=>array("name"=>"judul","type"=>"xsd:string"),
        "pengarang"=>array("name"=>"pengarang","type"=>"xsd:string"),
        "penerbit"=>array("name"=>"penerbit","type"=>"xsd:string"),
        "harga"=>array("name"=>"harga","type"=>"xsd:long")
    )
);

//fungsiGetListBuku
$server->wsdl->addComplexType(
    "booksArray",
    "complexType",
    "array",
    "",
    "SOAP-ENC:Array",
    array(),
    array(array("ref"=>"SOAP-ENC:arrayType","wsdl:arrayType"=>"tns:book[]")),
    "tns:book"
);

$server->register("fungsiGetListBuku",array(),array("return"=>"tns:booksArray"),"urn:BooksService","urn:BooksService#fungsiGetListBuku");

$HTTP_RAW_POST_DATA=isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : "";
$server->service($HTTP_RAW_POST_DATA);
?>



http://localhost:8080/belajar/webServices/webservicelp2maray/WebService/books3.php#

Lalu buat code u sisi client :
saya menyimpan code sisi server dengan nama books3.php
dimpan di htdocs/belajar/webServices/webservicelp2maray/client/books3.php
<?php
require("lib/nusoap.php");
?>
<html>
<head><title>lp2maray.com struct</title></head>
<body>

<?php
$url="http://localhost:8080/belajar/webServices/webservicelp2maray/WebService/books3.php";


    $client=new soapclient($url);
    $result=$client->call("fungsiGetListBuku",array("lp2m"=>""));

    $err=$client->getError();
    if($err){
        echo "<p><b>ERROR! ".$client->getError()."</p></b>";
    }
    else{
        if($result!=null){
            echo '<table border="1">
            <tr><th>Kode Buku</th><th>Judul</th><th>Pengarang</th><th>Penerbit</th><th>Harga</th></tr>';
            for($i=0;$i<sizeof($result);$i++){
                echo '<tr>
                <td>'.$result[$i]['id'].'</td>
                <td>'.$result[$i]['judul'].'</td>
                <td>'.$result[$i]['pengarang'].'</td>
                <td>'.$result[$i]['penerbit'].'</td>
                <td>'.$result[$i]['harga'].'</td>
                </tr>';
            }
            echo '</table>';
        }
        else{
            echo "<p><b>Kode buku tidak ditemukan!</b></p>";
        }
    }

?>
</body>
</html>

lalu jalankan:
http://localhost:8080/belajar/webServices/webservicelp2maray/client/books3.php
Selamat mencoba yaaaaaa

Nusoap Menampilkan Info Detail

Adapun codenya adalah sbb
Buat service disisi server
books2.php
misal sya menyimpannya di :htdocs/belajar/webServices/webservicelp2maray/WebService/books2.php#
<?php

function fungsiGetBukuDetail($bookId){
    mysql_connect("localhost","root","");
    mysql_select_db("webservice");

    $result=mysql_query("SELECT * FROM `buku` WHERE `id`='".$bookId."'");
    while($data=mysql_fetch_array($result)){
        $book=array(
            "id"=>$data['id'],
            "judul"=>$data['judul'],
            "pengarang"=>$data['pengarang'],
            "penerbit"=>$data['penerbit'],
            "harga"=>$data['harga']
        );
    }

    mysql_close();
    return $book;
}

require("lib/nusoap.php");
$server=new soap_server();
$server->configureWSDL("Books","urn:BooksService");//Books=Name & BooksService=Namespace

//fungsiGetBukuDetail
$server->wsdl->addComplexType(
    "book",
    "complexType",
    "struct",
    "all",
    "",
    array(
        "id"=>array("name"=>"id","type"=>"xsd:string"),
        "judul"=>array("name"=>"judul","type"=>"xsd:string"),
        "pengarang"=>array("name"=>"pengarang","type"=>"xsd:string"),
        "penerbit"=>array("name"=>"penerbit","type"=>"xsd:string"),
        "harga"=>array("name"=>"harga","type"=>"xsd:long")
    )
);

$server->register("fungsiGetBukuDetail",array("bookId"=>"xsd:string"),array("return"=>"tns:book"),"urn:BooksService","urn:BooksService#fungsiGetBukuDetail");

$HTTP_RAW_POST_DATA=isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : "";
$server->service($HTTP_RAW_POST_DATA);
?>

jalankan:
http://localhost:8080/belajar/webServices/webservicelp2maray/WebService/books2.php#


lalu buat code u sisi client
misal dengan nama yang sama books2.php
saya simpan di htdocs/belajar/webServices/webservicelp2maray/client/books2.php

<?php
require("lib/nusoap.php");
?>
<html>
<head><title>lp2maray.com</title></head>
<body>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
Kode Buku :&nbsp;<input type="text" name="txtKodeBuku">&nbsp;
<input type="submit" name="submit" value="CARI">
</form>
<?php
$url="http://localhost:8080/belajar/webServices/webservicelp2maray/WebService/books2.php";

if(isset($_POST['submit'])){
    $client=new soapclient($url);
    $result=$client->call("fungsiGetBukuDetail",array("bookId"=>$_POST['txtKodeBuku']));

    $err=$client->getError();
    if($err){
        echo "<p><b>ERROR! ".$client->getError()."</p></b>";
    }
    else{
        if($result!=null){
            echo "<p>
            <b>Kode buku</b> :&nbsp".$result['id']."<br>
            <b>Judul</b> :&nbsp".$result['judul']."<br>
            <b>Pengarang</b> :&nbsp".$result['pengarang']."<br>
            <b>Penerbit</b> :&nbsp".$result['penerbit']."<br>
            <b>Harga</b> :&nbsp".$result['harga']."</p>";
        }
        else{
            echo "<p><b>Kode buku tidak ditemukan!</b></p>";
        }
    }
}
?>
</body>
</html>

 

Hmm....Tambah Seru.....





Nusoap menampilkan ID Tabel

Buat tabel sbb


CREATE TABLE IF NOT EXISTS `buku` (
  `id` char(5) NOT NULL DEFAULT '',
  `judul` varchar(50) DEFAULT NULL,
  `pengarang` varchar(50) DEFAULT NULL,
  `penerbit` varchar(30) DEFAULT NULL,
  `harga` bigint(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data untuk tabel `buku`
--

INSERT INTO `buku` (`id`, `judul`, `pengarang`, `penerbit`, `harga`) VALUES
('B-001', 'Mewujudkan Mimpi Dalam 5 Menit', 'Riadi Marta Dinata', 'Penerbit lp2maray', 50000),
('B-002', 'Menguasai Sulam Dalam 1 Minggu', 'Rini Dyah Anggorowati', 'Penerbit lp2maray', 60000),
('B-003', '1001 Tips dan Trik SEO MAster', 'Khanza Humaira Dinata', 'Penerbit lp2maray', 45000);

buat code pada server:books1.php
misal saya simpan di htdocs/belajar/webServices/webservicelp2maray/WebService/books1.php

<?php

function fungsiGetBukuID(){
    mysql_connect("localhost","root","");
    mysql_select_db("webservice");
    $result=mysql_query("SELECT `id` FROM `buku`");

    $index=0;
    while($data=mysql_fetch_array($result)){
        $bookId[$index]=$data['id'];
        $index++;
    }
    mysql_close();
    return $bookId;//array
}

require("lib/nusoap.php");
$server=new soap_server();
$server->configureWSDL("Books","urn:BooksService");//Books=Name & BooksService=Namespace

//fungsiGetBukuID
$server->wsdl->addComplexType(
    "idArray",
    "complexType",
    "array",
    "",
    "SOAP-ENC:Array",
    array(),
    array(array("ref"=>"SOAP-ENC:arrayType","wsdl:arrayType"=>"xsd:string[]")),
    "xsd:string"
);

$server->register("fungsiGetBukuID",array(),array("return"=>"tns:idArray"),"urn:BooksService","urn:BooksService#fungsiGetBukuID");

$HTTP_RAW_POST_DATA=isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : "";
$server->service($HTTP_RAW_POST_DATA);
?>
Lalu jalankan
http://localhost:8080/belajar/webServices/webservicelp2maray/WebService/books1.php#


 Lalu buat clientnya
misal namanya sama:books1.php
saya simpan di htdocs/belajar/webServices/webservicelp2maray/client/books1.php
<?php
require("lib/nusoap.php");
?>
<html>
<head><title>lp2maray</title></head>
<body>

<?php
$url="http://localhost:8080/belajar/webServices/webservicelp2maray/WebService/books1.php";

    $client=new soapclient($url);
    $result=$client->call("fungsiGetBukuID",array("lp2m"=>""));

    $err=$client->getError();
    if($err){
        echo "<p><b>ERROR! ".$client->getError()."</p></b>";
    }
    else{
        echo "<p><b>Daftar ID Buku LP2MARAY:</b></p>";
        for($i=0;$i<sizeof($result);$i++){
            echo $result[$i]."<br>";
        }
    }

?>
</body>
</html>
jalankan :
http://localhost:8080/belajar/webServices/webservicelp2maray/client/books1.php





Sip deh....






Nusoap Fungsi Kurs

Source code disisi server

Misal :saya simpan di htdocs/belajar/webServices/webservicelp2maray/WebService/fungsiKurs.php

<?php

function fungsiKurs($dollar){
    return 13800*$dollar;
}

require("lib/nusoap.php");

$server=new soap_server();
$server->configureWSDL("Kurs","urn:kursService");//name,namespace

$server->register("fungsiKurs",array("dollar"=>"xsd:int"),array("return"=>"xsd:long"),"urn:kursService","urn:kursService#fungsiKurs");//namafungsi,input,output
$HTTP_RAW_POST_DATA=isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

?>
http://localhost:8080/belajar/webServices/webservicelp2maray/WebService/fungsiKurs.php
hasilnya tampak

lalu kode untuk menampilkan di sisi client
misal saya simpan di htdocs/belajar/webServices/webservicelp2maray/client/kurs-client.php

<?php
require("/lib/nusoap.php");
?>
<html>
<head><title>LP2MARAY</title></head>
<body>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
Dollar&nbsp;:&nbsp;<input type="text" name="txtDollar">&nbsp;<input type="submit" name="submit" value="KURS">
</form>
<?php
$url="http://localhost:8080/belajar/webServices/webservicelp2maray/WebService/fungsiKurs.php";

if(isset($_POST['submit'])){
    $client=new soapclient($url);
    $result=$client->call("fungsiKurs",array("dollar"=>$_POST['txtDollar']));

    $err=$client->getError();
    if($err){
        echo "<p><b>ERROR! ".$client->getError()."</p></b>";
    }
    else{
        echo "<p><b>Rupiah : $result</b></p>";
    }
/*
    echo "<hr>";
    echo "SOAP Request";
    echo "<pre>".htmlentities($client->request)."</pre>";
    echo "SOAP Response";
    echo "<pre>".htmlentities($client->response)."</pre>";
*/
}
?>
</body>
</html>


lalu coba jalankan :
http://localhost:8080/belajar/webServices/webservicelp2maray/client/kurs-client.php





Penegertian Web Service

Web service adalah suatu sistem yang mendukung interoperabilitas dan interaksi antar sistem pada suatu jaringan.

Teknologi web service menawarkan kemudahan menjembatani setiap informasi tanpa mempermasalahkan perbedaan teknologi yang digunakan oleh masing-masing sumber.
Web service menggunakan format XML dalam melakukan pertukaran data, sehingga dapat diakses oleh sistem lain walaupin berbeda platform, sistem operasi maupun berbeda bahasa pemrograman.

Web service bertujuan meningkatkan kolaborasi antar pemrogram dan perusahaan, yang memungkinkan sebuah fungsi di dalam web service dapat dipinjam oleh aplikasi lain tanpa perlu mengetahui detil pemrograman yang terdapat di dalamnya.


Ada beberapa pendekatan mengimplementasikan web service. Tiga yang paling banyak digunakan adalah Representational State Transfer (REST), XML-RPC, dan SOAP. Namun kebanyakan aplikasi enterprise menggunakan SOAP. Yang akan saya bahas dalam artikel kali ini adalah SOAP saja..

SOAP (Simple Object Access Protocol) merupakan protokol yang digunakan untuk mempertukarkan data atau informasi dalam format XML. SOAP dapat dikatakan sebagai gabungan antara HTTP dengan XML karena SOAP umumnya menggunakan protocol HTTP sebagai sarana transport datanya dan data akan dipertukarkan ditulis dalam format XML. Karena SOAP mengunakan HTTP dan XML maka SOAP memungkinkan pihak-pihak yang mempunyai platform, system operasi dan perangkat lunak yang berbeda dapat saling mempertukarkan datanya.

Dalam PHP sendiri terdapat suatu file library yang dapat digunakan untuk mencreate SOAP Sever yaitu menggunakan NuSOAP
silakan download di :new Nusoap

NuSOAP adalah sebuah kumpulan class-class PHP yang memungkinkan  user untuk mengirim dan menerima pesan SOAP melalui protokol HTTP. Salah satu keuntungan dari NuSOAP adalah penggunaannya tidak membutuhkan registrasi khusus ke Sistem Operasi maupun web server karena NuSOAP bukan merupakan PHP extension. NuSOAP ditulis dalam kode PHP murni sehingga semua developer web dapat mengunakan tool ini tanpa tergantung pada jenis web server yang digunakan.

contoh sederhana:
 basic1.php =>simpan difolder server
contoh : saya mengcopynya di folder :
htdocs/webServices/webservicelp2maray/WebService/basic1.php


<?php
// Pull in the NuSOAP code
require_once('lib\nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl', 'urn:hellowsdl');
// Register the method to expose
$server->register('hello',                // method name
    array('name' => 'xsd:string'),        // input parameters
    array('return' => 'xsd:string'),      // output parameters
    'urn:hellowsdl',                      // namespace
    'urn:hellowsdl#hello',                // soapaction
    'rpc',                                // style
    'encoded',                            // use
    'Says hello to the caller'            // documentation
);
// Define the method as a PHP function
function hello($name) {
 return 'Hello, ' . $name;
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
 jalankan:
http://localhost:8080/belajar/webServices/webservicelp2maray/WebService/basic1.php
 
 
 
Lalu copy file u client di
 
htdocs/belajar/webServices/webservicelp2maray/client/basic1.php
 
<?php
require_once('lib\nusoap.php');
$client = new soapclient('http://localhost:8080/belajar/webServices/webservicelp2maray/WebService/basic1.php');
$result = $client->call('hello', array('name' => 'http://www.p2maray.com'));
echo($result);
 
?>
 
Hasilnya
 
 
 
NB port 8080 adalah port default webserver yangs saya gunakan....untuk default yang lain adaah port 80
 
WAH.....Mudah yaaaaaaaaa:-)