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

Tidak ada komentar:

Posting Komentar