Minggu, 17 April 2016

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....






Tidak ada komentar:

Posting Komentar