Multiple (Multi-dimensional) Array in NuSoap
NuSoap – WSDL untuk data array, bagaimana caranya? mengirim variabel output array kepada aplikasi client. syntax xsd:array tidak ada. Ini contoh aplikasi sederhana untuk menampilkan data mahasiswa dari suatu jurusan. perhatikan tipe data ini :
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 | // 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=>’’) ) ‘jurusan’ => array(’name’ => ‘jurusan’, ‘type’ => ‘xsd:string’) ) ); // Output Complex Datatype $server->wsdl->addComplexType( ‘TypeDataOutput’, ‘complexType’, ’struct’, ‘all’, ”, array( ‘nim’ => array(’name’ => ‘nim’, ‘type’ => ‘xsd:int’), ‘nama’ => array(’name’ => ‘nama’, ‘type’ => ‘xsd:string’), ‘alamat’ => array(’name’ => ‘alamat’, ‘type’ => ‘xsd:string’) ) ); // Output Array Complex Datatype $server->wsdl->addComplexType(’TypeDataOutputArray’, ‘complexType’, ‘array’, ”, ‘SOAP-ENC:Array’, array(), array(array(’ref’=>’SOAP-ENC:arrayType’,'wsdl:arrayType’=>’tns:TypeDataOutput[]‘)), ‘tns:TypeDataOutput’ ); |
// 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=>’’) ) ‘jurusan’ => array(’name’ => ‘jurusan’, ‘type’ => ‘xsd:string’) ) ); // Output Complex Datatype $server->wsdl->addComplexType( ‘TypeDataOutput’, ‘complexType’, ’struct’, ‘all’, ”, array( ‘nim’ => array(’name’ => ‘nim’, ‘type’ => ‘xsd:int’), ‘nama’ => array(’name’ => ‘nama’, ‘type’ => ‘xsd:string’), ‘alamat’ => array(’name’ => ‘alamat’, ‘type’ => ‘xsd:string’) ) ); // Output Array Complex Datatype $server->wsdl->addComplexType(’TypeDataOutputArray’, ‘complexType’, ‘array’, ”, ‘SOAP-ENC:Array’, array(), array(array(’ref’=>’SOAP-ENC:arrayType’,'wsdl:arrayType’=>’tns:TypeDataOutput[]‘)), ‘tns:TypeDataOutput’ );
Dan ini contoh lengkapnya:
Server: multiarray.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 | configureWSDL('ws_multiplearray', 'urn:ws_multiplearray'); // ## 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=>'’) ) 'jurusan' => array('name' => 'jurusan', 'type' => 'xsd:string') ) ); // Output Complex Datatype $server->wsdl->addComplexType( 'TypeDataOutput', 'complexType', 'struct', 'all', '', array( 'nim' => array('name' => 'nim', 'type' => 'xsd:int'), 'nama' => array('name' => 'nama', 'type' => 'xsd:string'), 'alamat' => array('name' => 'alamat', 'type' => 'xsd:string') ) ); // Output Array Complex Datatype $server->wsdl->addComplexType('TypeDataOutputArray', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:TypeDataOutput[]')), 'tns:TypeDataOutput' ); // ## part III - register the methods // register the various methods to be called // Get Data Mahasiswa $server->register('GetDataMahasiswa', // method name array('return' => 'tns:TypeDataInput'),// input parameters array('return' => 'tns:TypeDataOutputArray'), // output parameters 'urn:ws_multiplearray', // namespace 'urn:ws_multiplearray#GetDataMahasiswa', // soapaction 'rpc', // style 'encoded', // use 'Get Data Mahasiswa ' // 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 GetDataMahasiswa($param) { // parameter values $jurusan = $param[jurusan] ; if($jurusan == 'mipa') { $loop = 5 ; } elseif($jurusan == 'komputer') { $loop = 10 ; } else { $loop = 3 ; } for ($i = 0; $i <= $loop; $i++) { $return[$i] = array( 'nim' => $i , 'nama' => 'NAMA ke - '.$i , 'alamat' => 'ALAMAT ke - '.$i ); } 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(); |
configureWSDL('ws_multiplearray', 'urn:ws_multiplearray');
// ## 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=>'’) )
'jurusan' => array('name' => 'jurusan', 'type' => 'xsd:string')
)
);
// Output Complex Datatype
$server->wsdl->addComplexType(
'TypeDataOutput',
'complexType',
'struct',
'all',
'',
array(
'nim' => array('name' => 'nim', 'type' => 'xsd:int'),
'nama' => array('name' => 'nama', 'type' => 'xsd:string'),
'alamat' => array('name' => 'alamat', 'type' => 'xsd:string')
)
);
// Output Array Complex Datatype
$server->wsdl->addComplexType('TypeDataOutputArray',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:TypeDataOutput[]')),
'tns:TypeDataOutput'
);
// ## part III - register the methods
// register the various methods to be called
// Get Data Mahasiswa
$server->register('GetDataMahasiswa', // method name
array('return' => 'tns:TypeDataInput'),// input parameters
array('return' => 'tns:TypeDataOutputArray'), // output parameters
'urn:ws_multiplearray', // namespace
'urn:ws_multiplearray#GetDataMahasiswa', // soapaction
'rpc', // style
'encoded', // use
'Get Data Mahasiswa ' // 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 GetDataMahasiswa($param)
{
// parameter values
$jurusan = $param[jurusan] ;
if($jurusan == 'mipa') {
$loop = 5 ;
} elseif($jurusan == 'komputer') {
$loop = 10 ;
} else {
$loop = 3 ;
}
for ($i = 0; $i <= $loop; $i++) {
$return[$i] = array(
'nim' => $i ,
'nama' => 'NAMA ke - '.$i ,
'alamat' => 'ALAMAT ke - '.$i
);
}
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: ClientArray.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Pull in the NuSOAP code require_once('./include/nusoap.php'); // Create the client instance $client = new soapclient('http://localhost/test/multiplearray.php?wsdl', true); // Check for an error $err = $client->getError(); if ($err) { // Display the error echo ' <h2>Constructor error</h2> <pre>' . $err . ' |
// Pull in the NuSOAP code
require_once('./include/nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/test/multiplearray.php?wsdl', true);
// Check for an error
$err = $client->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('jurusan' => $_GET[jurusan]);
$result = $proxy->GetDataMahasiswa($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) . '
';
Related Posts:
25.Sep.08
PHP, Web Service
You can leave a response, or trackback from your own site.
Your blog is interesting!
Keep up the good work!