Anda di halaman 1dari 6

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;

$nm. .$prc) { mysql_connect("localhost".$dsc. mysql_query("INSERT INTO tb (id. $index++."root". dsc."'. "nm"=>$data['nm'].$id.dt='".dsc='".$nm.$dt. return $tblist. "dsc"=>$data['dsc'].$nm."'. dt."").$nm. } function updateTb($id."' WHERE id='".$dt.$id.$prc) { mysql_connect("localhost".while ($data=mysql_fetch_array($result)){ $tblist[$index]=array( "id"=>$data['id']. mysql_query("UPDATE tb SET nm='".$prc.$dsc."'. } mysql_close()."'"). nm. prc) VALUES ('".'"."'.$dt. mysql_select_db("db"). return "Succeed".prc='". "dt"=>$data['dt'].'"."')").'".$dsc. } function insertTb($id."'."")."'.$dsc. "prc"=>$data['prc'] ). return "Succeed". mysql_select_db("db")."root"."'.$dt.'".$prc.

array( "id"=>array("name"=>"id".} function deleteTb($id) { mysql_connect("localhost". $server->wsdl->addcomplextype( ."")."type"=>"xsd:string")."type"=>"xsd:string"). mysql_select_db("db"). "struct". "all". } require("lib/nusoap."root"."urn:WebServ"). "prc"=>array("name"=>"prc". "complextype". $server->configureWSDL("Serv"."type"=>"xsd:string") ) )."type"=>"xsd:string"). $server= new soap_server(). "dsc"=>array("name"=>"dsc".php"). "dt"=>array("name"=>"dt". "nm"=>array("name"=>"nm". $server->wsdl->addcomplextype( "outputarray". mysql_query("DELETE FROM tb WHERE id = '". return "Succeed"."type"=>"xsd:string").$id. ""."'").

$server->register( "getTb". . "rpc". $server->register( "insertTb". "urn:WebServ". "" ). "SOAP-ENC:Array". "". "complextype". "urn:WebServ#getTb"."dsc"=>"xsd:string". "wsdl:arrayType"=>"tns:outputarray[]") ). "encoded". "array". array(). array(). "tns:outputarray" ). array("return"=>"tns:outarray"). array("id"=>"xsd:string". array("return"=>"tns:outarray"). array( array("ref"=>"SOAP-ENC:arrayType". "urn:WebServ"."outarray". "dt"=>"xsd:string"."nm"=>"xsd:string"."prc"=>"xsd:string").

"rpc". array("id"=>"xsd:string"). "rpc". $server->register( "updateTb"."prc"=>"xsd:string"). "dt"=>"xsd:string". $server->register( "deleteTb". "urn:WebServ". "" ). "rpc". array("id"=>"xsd:string". "encoded". array("return"=>"tns:outarray"). "urn:WebServ". "" ). "encoded". array("return"=>"tns:outarray")."dsc"=>"xsd:string"."urn:WebServ#insertTb". $HTTP_RAW_POST_DATA=isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : ""."nm"=>"xsd:string". "urn:WebServ#updateTb". "urn:WebServ#deleteTb". "" ). "encoded". .

$server->service($HTTP_RAW_POST_DATA). ?> .