PHP WebService Programming with NuSOAP Using WSDL
Sekedar referensi untuk WebService dan implementasi webservice di PHP itu baca dan gunakan NuSoap bisa download di : http://dietrich.ganx4.com/nusoap/
Trus sebelum mencoba, baca dulu refernsi ini :
http://www.wackylabs.net/?submit=Search&s=nusoap
http://www.scottnichol.com/soap.htm
ini contoh scriptnya:
Server : myserver.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | // ## part I - setup // includes the nusoap.php file, sets up the namespace and wsdl object // Pull in the NuSOAP code require_once('nusoap.php'); // Create the server instance $server = new soap_server(); // Initialize WSDL support $server->configureWSDL('zodiakserver', 'urn:zodiakserver'); // ## part II - registering types // registers the types || Register the data structures used by the service // Input Complex Datatype $server->wsdl->addComplexType ( 'TypeDataInput', // name 'complexType', // typeClass (complexType|simpleType|attribute) 'struct', // phpType: currently supported are array and struct (php assoc array) 'all', // compositor (all|sequence|choice) '', // restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array) array( // elements = array ( name = array(name=>'’,type=>'’) ) 'nama_zodiak' => array('name' => 'nama_zodiak', 'type' => 'xsd:string') ) ); // Output Complex Datatype $server->wsdl->addComplexType ( 'TypeDataOutput', // name 'complexType', // typeClass (complexType|simpleType|attribute) 'struct', // phpType: currently supported are array and struct (php assoc array) 'all', // compositor (all|sequence|choice) '', // restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array) array( // elements = array ( name = array(name=>'’,type=>'’) ) 'nama_zodiak' => array('name' => 'nama_zodiak', 'type' => 'xsd:string'), 'tanggal' => array('name' => 'tanggal', 'type' => 'xsd:string'), 'ramalan' => array('name' => 'ramalan', 'type' => 'xsd:string'), 'keuangan' => array('name' => 'keuangan', 'type' => 'xsd:string'), 'kesehatan' => array('name' => 'kesehatan', 'type' => 'xsd:string'), 'angka_keberuntungan' => array('name' => 'angka_keberuntungan', 'type' => 'xsd:string') ) ); // ## part III - register the methods // register the various methods to be called // RamalanZodiak $server->register('RamalanZodiak', // method name array('param' => 'tns:TypeDataInput'), // input parameters array('return' => 'tns:TypeDataOutput'), // output parameters 'urn:zodiakserver', // namespace 'urn:zodiakserver#RamalanZodiak', // soapaction 'rpc', // style 'encoded', // use 'Untuk Meramal Zodiak Neh' // documentation ); // ## part IV - define the functions. // finally, define the functions - this can be done in a seperate include file to simplify some of the layout of the file. // Define the method as a PHP function function RamalanZodiak($param) { // Database Connection include("./include/_koneksi.php") ; // ## VARIABLES, CONSTANTA // parameter values $nama_zodiak = $param[nama_zodiak] ; //not null parameter check if (empty($nama_zodiak)) { return new soap_fault('Client', '', 'Parameter harus ada nilainya!', ''); } //## SECURITY CHECK //sql injection protection $nama_zodiak = mysql_escape_string($nama_zodiak) ; // sql and conditions $tablenames = "ramalan" ; $condition = "WHERE nama_zodiak = '$nama_zodiak' " ; // Query Processing $sql = "SELECT * FROM $tablenames $condition " ; $query = mysql_query($sql) or die(mysql_errno() . ": " . mysql_error()); if ($result = mysql_fetch_array($query) ) { $return = array( 'nama_zodiak' => $result[nama_zodiak] , 'tanggal' => $result[tanggal] , 'ramalan' => $result[ramalan] , 'keuangan' => $result[keuangan] , 'kesehatan' => $result[kesehatan] , 'angka_keberuntungan' => $result[angka_keberuntungan] ); } else { //default value if no rows found [optional] $return = array( 'nama_zodiak' => 'N/A' , 'tanggal' => 'N/A' , 'ramalan' => 'N/A' , 'keuangan' => 'N/A' , 'kesehatan' => 'N/A' , 'angka_keberuntungan' => 'N/A' ); } // Output Values return $return ; } // ## Finally, we pass whatever data the php page receives into the soap processor and see what happens: // Use the request to (try to) invoke the service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA); exit(); |
// ## part I - setup
// includes the nusoap.php file, sets up the namespace and wsdl object
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('zodiakserver', 'urn:zodiakserver');
// ## part II - registering types
// registers the types || Register the data structures used by the service
// Input Complex Datatype
$server->wsdl->addComplexType
(
'TypeDataInput', // name
'complexType', // typeClass (complexType|simpleType|attribute)
'struct', // phpType: currently supported are array and struct (php assoc array)
'all', // compositor (all|sequence|choice)
'', // restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
array( // elements = array ( name = array(name=>'’,type=>'’) )
'nama_zodiak' => array('name' => 'nama_zodiak', 'type' => 'xsd:string')
)
);
// Output Complex Datatype
$server->wsdl->addComplexType
(
'TypeDataOutput', // name
'complexType', // typeClass (complexType|simpleType|attribute)
'struct', // phpType: currently supported are array and struct (php assoc array)
'all', // compositor (all|sequence|choice)
'', // restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
array( // elements = array ( name = array(name=>'’,type=>'’) )
'nama_zodiak' => array('name' => 'nama_zodiak', 'type' => 'xsd:string'),
'tanggal' => array('name' => 'tanggal', 'type' => 'xsd:string'),
'ramalan' => array('name' => 'ramalan', 'type' => 'xsd:string'),
'keuangan' => array('name' => 'keuangan', 'type' => 'xsd:string'),
'kesehatan' => array('name' => 'kesehatan', 'type' => 'xsd:string'),
'angka_keberuntungan' => array('name' => 'angka_keberuntungan', 'type' => 'xsd:string')
)
);
// ## part III - register the methods
// register the various methods to be called
// RamalanZodiak
$server->register('RamalanZodiak', // method name
array('param' => 'tns:TypeDataInput'), // input parameters
array('return' => 'tns:TypeDataOutput'), // output parameters
'urn:zodiakserver', // namespace
'urn:zodiakserver#RamalanZodiak', // soapaction
'rpc', // style
'encoded', // use
'Untuk Meramal Zodiak Neh' // documentation
);
// ## part IV - define the functions.
// finally, define the functions - this can be done in a seperate include file to simplify some of the layout of the file.
// Define the method as a PHP function
function RamalanZodiak($param)
{
// Database Connection
include("./include/_koneksi.php") ;
// ## VARIABLES, CONSTANTA
// parameter values
$nama_zodiak = $param[nama_zodiak] ;
//not null parameter check
if (empty($nama_zodiak))
{
return new soap_fault('Client', '', 'Parameter harus ada nilainya!', '');
}
//## SECURITY CHECK
//sql injection protection
$nama_zodiak = mysql_escape_string($nama_zodiak) ;
// sql and conditions
$tablenames = "ramalan" ;
$condition = "WHERE nama_zodiak = '$nama_zodiak' " ;
// Query Processing
$sql = "SELECT *
FROM $tablenames
$condition " ;
$query = mysql_query($sql) or die(mysql_errno() . ": " . mysql_error());
if ($result = mysql_fetch_array($query) )
{
$return = array(
'nama_zodiak' => $result[nama_zodiak] ,
'tanggal' => $result[tanggal] ,
'ramalan' => $result[ramalan] ,
'keuangan' => $result[keuangan] ,
'kesehatan' => $result[kesehatan] ,
'angka_keberuntungan' => $result[angka_keberuntungan]
);
}
else
{
//default value if no rows found [optional]
$return = array(
'nama_zodiak' => 'N/A' ,
'tanggal' => 'N/A' ,
'ramalan' => 'N/A' ,
'keuangan' => 'N/A' ,
'kesehatan' => 'N/A' ,
'angka_keberuntungan' => 'N/A'
);
}
// Output Values
return $return ;
}
// ## Finally, we pass whatever data the php page receives into the soap processor and see what happens:
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
exit();Client : myclient.php
1 2 3 4 5 6 7 | getError(); if ($err) { // Display the error echo ' <h2>Constructor error</h2> <pre>' . $err . ' |
getError();
if ($err)
{
// Display the error
echo '
<h2>Constructor error</h2>
<pre>' . $err . '';
// At this point, you know the call that follows will fail
}
// Create the proxy
$proxy = $client->getProxy();
// Call the SOAP method
$param = array('nama_zodiak' => $_GET[nama_zodiak]);
$result = $proxy->RamalanZodiak($param);
// Check for a fault
if ($proxy->fault)
{
echo '
Fault
1 2 3 | '; print_r($result); echo ' |
'; print_r($result); echo '
';
}
else
{
// Check for errors
$err = $proxy->getError();
if ($err)
{
// Display the error
echo '
Error
1 | ' . $err . ' |
' . $err . '
';
}
else
{
// Display the result
echo '
Result
1 2 3 | '; print_r($result); echo ' |
'; print_r($result); echo '
';
}
}
// ## OPTIONAL
// Display the request and response
echo '
Request
';
echo '
1 | ' . htmlspecialchars($proxy->request, ENT_QUOTES) . ' |
' . htmlspecialchars($proxy->request, ENT_QUOTES) . '
';
echo '
Response
';
echo '
1 | ' . htmlspecialchars($proxy->response, ENT_QUOTES) . ' |
' . htmlspecialchars($proxy->response, ENT_QUOTES) . '
';
// Display the debug messages
echo '
Debug
';
echo '
1 | ' . htmlspecialchars($proxy->debug_str, ENT_QUOTES) . ' |
' . htmlspecialchars($proxy->debug_str, ENT_QUOTES) . '
';
?>
Database Connection : _koneksi.php
Related Posts:
Tags: nusoap, soap, webservice, wsdl
18.Oct.08
PHP, Web Service
You can leave a response, or trackback from your own site.
[...] sich teknologi web service itu? web service adalah sebuah sistem yang dirancang untuk dapat [...]