Tampilkan postingan dengan label Web - PHP. Tampilkan semua postingan
Tampilkan postingan dengan label Web - PHP. Tampilkan semua postingan

Rabu, 02 Mei 2012

Web Service Client PHP NUSOAP Mengakses Web Service C#.NET

Web Service Client - PHP Mengakses (Consume) Web Service C#.NET 

Web Service Client ini akan mengkonsumsi Web Service C#.NET seperti yang diterangkan pada halaman Web Service C#.NET

Copy library NUSOAP ke dalam direktori lib

Buat file webservice wsclientasp.php, tuliskan kode program berikut :

<html>

<head></head>

<body>

<table width="700" border="1" cellspacing="2" cellpadding="3">

<tr>

<td>ID</td>

<td>Name</td>

<td>Description</td>

<td>Date</td>

<td>Price</td>

<td>&nbsp;</td>

</tr>

<?php

require_once('lib/nusoap.php');

$client = new soapclient('http://localhost:1495/ws/Service.asmx?wsdl', true);

$params = '';

$result = $client->call("getTb", $params);

//print_r($result['getTbResult']['diffgram']['NewDataSet']['admin']); //[0]['Nama']);

for ($i=0;$i<count($result['getTbResult']['diffgram']['NewDataSet']['tb']);$i++)

{

?>

<tr>

<td><?php echo $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['id']; ?>&nbsp;</td>

<td><?php echo $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['nm']; ?>&nbsp;</td>

<td><?php echo $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['dsc']; ?>&nbsp;</td>

<td><?php echo $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['dt']; ?>&nbsp;</td>

<td><?php echo $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['prc']; ?>&nbsp;</td>

<td><a href="wsclientaspedit.php?id=<?php echo $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['id']; ?>">Edit</a> <a href="wsclientaspdelete.php?id=<?php echo $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['id']; ?>">Del</a> </td>

</tr>

<?php

}

?>

</table>



<br />

<a href="wsclientaspadd.php">Add</a>

</body>

</html>


Buat file webservice wsclientaspadd.php, tuliskan kode program berikut :

<?php

if ($_POST['button'] == 'Save')

{

require('lib/nusoap.php');

$client = new soapclient('http://localhost:1495/ws/Service.asmx?wsdl', true);

$result=$client->call("insertTb",

array("id"=>$_POST['id'],"nm"=>$_POST['nm'],"dsc"=>$_POST['dsc'],"dt"=>$_POST['dt'],"prc"=>$_POST['prc']));

$err=$client->getError();

if($err){

echo "error";

}

header("location:wsclientasp.php");

}

?>

<html>

<head></head>

<body>

<form id="form1" name="form1" method="post" action="">

<table width="300" border="1" cellspacing="2" cellpadding="3">

<tr>

<td>ID</td>

<td><label>

<input name="id" type="text" id="id" />

</label></td>

</tr>

<tr>

<td>Name</td>

<td><label>

<input name="nm" type="text" id="nm" />

</label></td>

</tr>

<tr>

<td>Description</td>

<td><label>

<input name="dsc" type="text" id="dsc" />

</label></td>

</tr>

<tr>

<td>Date</td>

<td><label>

<input name="dt" type="text" id="dt" />

</label></td>

</tr>

<tr>

<td>Price</td>

<td><label>

<input name="prc" type="text" id="prc" />

</label></td>

</tr>

<tr>

<td>&nbsp;</td>

<td><label>

<input name="button" type="submit" id="button" value="Save" />

</label></td>

</tr>

</table>

</form>

</body>

</html>


Buat file webservice wsclientaspedit.php, tuliskan kode program berikut :

<?php

if ($_POST['button'] == 'Save')

{

require('lib/nusoap.php');

$client = new soapclient('http://localhost:1495/ws/Service.asmx?wsdl', true);

$result=$client->call("updateTb",

array("id"=>$_POST['id'],"nm"=>$_POST['nm'],"dsc"=>$_POST['dsc'],"dt"=>$_POST['dt'],"prc"=>$_POST['prc']));

$err=$client->getError();

if($err){

echo "error";

}

header("location:wsclientasp.php");

}

?>

<html>

<head></head>

<body>

<?php

require_once('lib/nusoap.php');

$client = new soapclient('http://localhost:1495/ws/Service.asmx?wsdl', true);

$params = '';

$result = $client->call("getTb", $params);

//print_r($result['getAdminResult']['diffgram']['NewDataSet']['admin']); //[0]['Nama']);



if($result!=null){

for ($i=0;$i<count($result['getTbResult']['diffgram']['NewDataSet']['tb']);$i++){

if ($result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['id'] == $_GET['id'])

{

$id = $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['id'];

$nm = $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['nm'];

$dsc = $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['dsc'];

$dt = $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['dt'];

$prc = $result['getTbResult']['diffgram']['NewDataSet']['tb'][$i]['prc'];

}

}

}

?>

<form id="form1" name="form1" method="post" action="">

<table width="300" border="1" cellspacing="2" cellpadding="3">

<tr>

<td>ID</td>

<td><label>

<input name="id" type="text" id="id" value="<?php echo $id; ?>" readonly />

</label></td>

</tr>

<tr>

<td>Name</td>

<td><label>

<input name="nm" type="text" id="nm" value="<?php echo $nm; ?>" />

</label></td>

</tr>

<tr>

<td>Description</td>

<td><label>

<input name="dsc" type="text" id="dsc" value="<?php echo $dsc; ?>" />

</label></td>

</tr>

<tr>

<td>Date</td>

<td><label>

<input name="dt" type="text" id="dt" value="<?php echo $dt; ?>" />

</label></td>

</tr>

<tr>

<td>Price</td>

<td><label>

<input name="prc" type="text" id="prc" value="<?php echo $prc; ?>" />

</label></td>

</tr>

<tr>

<td>&nbsp;</td>

<td><label>

<input name="button" type="submit" id="button" value="Save" />

</label></td>

</tr>

</table>

</form>

</body>

</html>


Buat file webservice wsclientaspdelete.php, tuliskan kode program berikut :

<?php

require_once('lib/nusoap.php');

$client = new soapclient('http://localhost:1495/ws/Service.asmx?wsdl', true);

$result=$client->call("deleteTb",

array("id"=>$_GET['id']));

$err=$client->getError();

if($err){

echo "error";

}

header("location:wsclientasp.php");

?>


Hasilnya dapat dilihat pada url : localhost/wsclient/wsclientasp.php

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