Rabu, 02 Mei 2012

Web Service Database dengan PHP NUSOAP

Web Service - PHP NUSOAP

Install XAMPP, kemudian buka file konfigurasi php.ini, hilangkan, atau tidak usah aktifkan (comment) baris berikut :

;extension=php_soap.dll

Restart Apache (XAMPP), kemuadian jalankan localhost/phpmyadmin, create a database db and table tb:

create database db;

use db;


CREATE TABLE IF NOT EXISTS `tb` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`nm` varchar(200) DEFAULT NULL,

`dsc` text,

`dt` date DEFAULT NULL,

`prc` double DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


INSERT INTO `tb` (`id`, `nm`, `dsc`, `dt`, `prc`) VALUES

(1, 'A', 'BB', '2011-03-01', 100),

(2, 'C', 'DD', '2011-03-22', 200);

Copykan librari nusoap ke direktori lib.



Kemudian buat file webservice dengan nama ws.php, tuliskan kode program berikut:





<?php

function getTb(){

mysql_connect("localhost","root","");

mysql_select_db("db");


$result=mysql_query("SELECT * FROM tb");


$index=0;

while ($data=mysql_fetch_array($result)){

$tblist[$index]=array(

"id"=>$data['id'],

"nm"=>$data['nm'],

"dsc"=>$data['dsc'],

"dt"=>$data['dt'],

"prc"=>$data['prc']

);

$index++;

}


mysql_close();

return $tblist;

}


function insertTb($id,$nm,$dsc,$dt,$prc)

{

mysql_connect("localhost","root","");

mysql_select_db("db");

mysql_query("INSERT INTO tb (id, nm, dsc, dt, prc) VALUES ('".$id."','".$nm."','".$dsc."','".$dt."','".$prc."')");

return "Succeed";

}


function updateTb($id,$nm,$dsc,$dt,$prc)

{

mysql_connect("localhost","root","");

mysql_select_db("db");

mysql_query("UPDATE tb SET nm='".$nm."',dsc='".$dsc."',dt='".$dt."',prc='".$prc."' WHERE id='".$id."'");

return "Succeed";

}


function deleteTb($id)

{

mysql_connect("localhost","root","");

mysql_select_db("db");

mysql_query("DELETE FROM tb WHERE id = '".$id."'");

return "Succeed";

}


require("lib/nusoap.php");

$server= new soap_server();

$server->configureWSDL("Serv","urn:WebServ");



$server->wsdl->addcomplextype(

"outputarray",

"complextype",

"struct",

"all",

"",

array(

"id"=>array("name"=>"id","type"=>"xsd:string"),

"nm"=>array("name"=>"nm","type"=>"xsd:string"),

"dsc"=>array("name"=>"dsc","type"=>"xsd:string"),

"dt"=>array("name"=>"dt","type"=>"xsd:string"),

"prc"=>array("name"=>"prc","type"=>"xsd:string")

)

);


$server->wsdl->addcomplextype(

"outarray",

"complextype",

"array",

"",

"SOAP-ENC:Array",

array(),

array(

array("ref"=>"SOAP-ENC:arrayType",

"wsdl:arrayType"=>"tns:outputarray[]")

),

"tns:outputarray"

);


$server->register(

"getTb",

array(),

array("return"=>"tns:outarray"),

"urn:WebServ",

"urn:WebServ#getTb",

"rpc",

"encoded",

""

);


$server->register(

"insertTb",

array("id"=>"xsd:string","nm"=>"xsd:string","dsc"=>"xsd:string",

"dt"=>"xsd:string","prc"=>"xsd:string"),

array("return"=>"tns:outarray"),

"urn:WebServ",

"urn:WebServ#insertTb",

"rpc",

"encoded",

""

);


$server->register(

"updateTb",

array("id"=>"xsd:string","nm"=>"xsd:string","dsc"=>"xsd:string",

"dt"=>"xsd:string","prc"=>"xsd:string"),

array("return"=>"tns:outarray"),

"urn:WebServ",

"urn:WebServ#updateTb",

"rpc",

"encoded",

""

);


$server->register(

"deleteTb",

array("id"=>"xsd:string"),

array("return"=>"tns:outarray"),

"urn:WebServ",

"urn:WebServ#deleteTb",

"rpc",

"encoded",

""

);


$HTTP_RAW_POST_DATA=isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : "";


$server->service($HTTP_RAW_POST_DATA);

?>

Hasilnya dapat dilihat dan diakses di url: localhost/ws/ws.php