PHP gevorderde |
|
beste allemaal,
Ik heb de volgende webservice gemaakt:
Server:
<?php
// Insert the NuSOAP code
require_once('nusoap.php');
// Create an instance of the server
$server = new soap_server;
// Initialize WSDL support
$server->configureWSDL('helloworldwsdl', 'urn:helloworldwsdl');
// Put the WSDL schema types in the namespace with the tns prefix
$server->wsdl->schemaTargetNamespace = 'urn:helloworldwsdl';
// Register the data structures used by the service
$server->wsdl->addComplexType(
'Person',
'complexType',
'struct',
'all',
'',
array(
'firstname' => array('name' => 'firstname',
'type' => 'xsd:string'),
'age' => array('name' => 'age', 'type' => 'xsd:int'),
)
);
$server->wsdl->addComplexType(
'AgeConfirmReply',
'complexType',
'struct',
'all',
'',
array(
'output_string' => array('name' => 'output_string',
'type' => 'xsd:string'),
'allow' => array('name' => 'allow', 'type' => 'xsd:boolean')
)
);
// Register the method to expose
$server->register('hello', // method name
array('person' => 'tns:Person'), // input parameters
array('return' => 'tns:AgeConfirmReply'), // output parameters
'urn:helloworldwsdl', // namespace
'urn:helloworldwsdl#hello', // soapaction
'rpc', // style
'encoded', // use
'Confirm the enquirer is over 18' // documentation
);
// This is the method
function hello($input) {
$output_string = 'Hello ' . $input['firstname'] .
'. You are ' . $input['age'] . ' years old.';
if ( $input['age'] >= 18 ) { $allow = 1; }
return array(
'output_string' => $output_string,
'allow' => $allow
);
}
// This returns the result
$server->service($HTTP_RAW_POST_DATA);
?>
<?php // Insert the NuSOAP code require_once('nusoap.php'); // Create an instance of the server $server = new soap_server; // Initialize WSDL support $server->configureWSDL('helloworldwsdl', 'urn:helloworldwsdl'); // Put the WSDL schema types in the namespace with the tns prefix $server->wsdl->schemaTargetNamespace = 'urn:helloworldwsdl'; // Register the data structures used by the service $server->wsdl->addComplexType( 'Person', 'complexType', 'struct', 'all', '', 'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'), 'age' => array('name' => 'age', 'type' => 'xsd:int'), ) ); $server->wsdl->addComplexType( 'AgeConfirmReply', 'complexType', 'struct', 'all', '', 'output_string' => array('name' => 'output_string', 'type' => 'xsd:string'), 'allow' => array('name' => 'allow', 'type' => 'xsd:boolean') ) ); // Register the method to expose $server->register('hello', // method name array('person' => 'tns:Person'), // input parameters array('return' => 'tns:AgeConfirmReply'), // output parameters 'urn:helloworldwsdl', // namespace 'urn:helloworldwsdl#hello', // soapaction 'rpc', // style 'encoded', // use 'Confirm the enquirer is over 18' // documentation ); // This is the method function hello($input) { $output_string = 'Hello ' . $input['firstname'] . '. You are ' . $input['age'] . ' years old.'; if ( $input['age'] >= 18 ) { $allow = 1; } 'output_string' => $output_string, 'allow' => $allow ); } // This returns the result $server->service($HTTP_RAW_POST_DATA); ?>
Client:
<?php
// Insert the NuSOAP code
require_once('nusoap.php');
// This is location of the remote service
$client = new soapclient('http://www.domein.com/api/helloworld-server-wsdl.php?wsdl', true);
// Check for any errors from the remote service
$err = $client->getError();
if ($err) {
echo '<p><b>Error: ' . $err . '</b></p>';
}
$person = array('firstname' => 'Fred', 'age' => 22);
$result = $client->call('hello',array('message' => $person));
// Check for any faults reported by the remote service
if ($client->fault) {
echo '<p><b>Fault: ';
print_r($result);
echo '</b></p>';
} else {
// Check for any errors from the remote service
$err = $client->getError();
if ($err) {
echo '<p><b>Error: ' . $err . '</b></p>';
} else {
// If everything is OK display the result
print $result['output_string'] . ' ';
if ( $result['allow'] == 1 ) {
print "You may continue...";
} else {
print "You are too young!";
}
}
}
// Display the request and response
echo '<h2>Request</h2>';
$text = htmlspecialchars($client->request, ENT_QUOTES);
$remove = array ("<"," x");
$insert = array ("<br><","<br> x");
$text = str_replace($remove, $insert, $text);
echo '<pre>' . $text . '</pre>';
echo '<h2>Response</h2>';
$text = htmlspecialchars($client->response, ENT_QUOTES);
$remove = array ("<"," x");
$insert = array ("<br><","<br> x");
$text = str_replace($remove, $insert, $text);
echo '<pre>' . $text . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
<?php // Insert the NuSOAP code require_once('nusoap.php'); // This is location of the remote service $client = new soapclient('http://www.domein.com/api/helloworld-server-wsdl.php?wsdl', true); // Check for any errors from the remote service $err = $client->getError(); if ($err) { echo '<p><b>Error: ' . $err . '</b></p>'; } $person = array('firstname' => 'Fred', 'age' => 22); $result = $client->call('hello',array('message' => $person)); // Check for any faults reported by the remote service if ($client->fault) { } else { // Check for any errors from the remote service $err = $client->getError(); if ($err) { echo '<p><b>Error: ' . $err . '</b></p>'; } else { // If everything is OK display the result print $result['output_string'] . ' '; if ( $result['allow'] == 1 ) { print "You may continue..."; } else { print "You are too young!"; } } } // Display the request and response $remove = array ("<"," x"); $insert = array ("<br><","<br> x"); echo '<pre>' . $text . '</pre>'; echo '<h2>Response</h2>'; $remove = array ("<"," x"); $insert = array ("<br><","<br> x"); echo '<pre>' . $text . '</pre>'; // Display the debug messages ?>
Al ik de client webpagina open, dan krijg ik de volgende error:
Error: wsdl error: XML error parsing WSDL from http://www.dome...l.php?wsdl on line 2: Reserved XML Name
Heeft iemand enig idee van wat hier fout gaat?
Grt,
Roy
|