I am trying to write a webservice to consume an xml data string and I am having a little trouble, thought I would post my code here and see if any one had any ideas.
the server method is supposed to accept a XML string and then parse the data and do what ever with it. Well I can make the method call from my client and the request looks ok but I cant seem to get the xml string to show up so I can parse it.
//Server code
//client code
This is the actual xml I am trying to consume if you have any thoughts on a better method.
the server method is supposed to accept a XML string and then parse the data and do what ever with it. Well I can make the method call from my client and the request looks ok but I cant seem to get the xml string to show up so I can parse it.
//Server code
Code:
<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl2', 'urn:hellowsdl2');
// Register the data structures used by the service
$server->wsdl->addComplexType(
'xmlString',
'complexType',
'struct',
'all',
'',
array(
'firstname' => array('name' => 'firstname', 'type' => 'xsd:string')
)
);
// Register the method to expose
$server->register('ServicesRequest', // method name
array('xmlData' => 'tns:xmlString'), // input parameters
array('return' => 'xsd:string'), // output parameters
'urn:hellowsdl2', // namespace
'urn:hellowsdl2#hello', // soapaction
'rpc', // style
'encoded', // use
'' // documentation
);
// Define the method as a PHP function
function ServicesRequest($person) {
//parse xml
return "".$person['firstname']; //returns xml string to test if recieved ok.
}
// 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);
?>
//client code
Code:
<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('serverurl.com/testserver.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$person = array('firstname' => '<First Sal="MR" />firstname</First>');
$result = $client->call('ServicesRequest', array('person' => $person));
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
echo "this is".$result;
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
This is the actual xml I am trying to consume if you have any thoughts on a better method.
Code:
<?xml version="1.0"?>
<Order id="" un="" pw="">
<Person FirstName="" LastName="" SSN="" MaritalStatus="" BirthDate="" Age=""/>
<Phone PhoneType="" AreaCode="" Exchange="" Number="" Extension=""/>
<Email EmailType="" EmailAddress=""/>
</Order>