How can i set attributes with NuSoap in PHP? - php

I am working with nuSoap to build a soap server.
But i cant get the attributes the way i want.
I would like as return value:
<return xsi:type="tns:Taxatie">
<EmailAdres OptIn="1" xsi:type="tns:string">email#domain.com</EmailAdres>
</return>
And i get:
<return xsi:type="tns:Taxatie">
<EmailAdres OptIn="1" xsi:type="tns:EmailAdres">
<EmailAdres xsi:type="xsd:string">email#domain.com</EmailAdres>
</EmailAdres>
</return>
anyone know what i must change?
Or how I should set up the arrays?
This is my test code:
<?php
require_once("nusoap.php");
$soapserver = new nusoap_server();
$soapserver->configureWSDL('thijs.test', 'urn:thijs.test');
$soapserver->wsdl->addComplexType(
'Taxatie',
'complexType',
'struct',
'all',
'',
array(
'EmailAdres' => array('name' => 'EmailAdres', 'type' => 'tns:EmailAdres')
)
);
$soapserver->wsdl->addComplexType(
'EmailAdres',
'simpleType',
'struct',
'all',
'',
array(
"EmailAdres" => array('name' => 'EmailAdres', 'type' => 'xsd:string', 'minOccurs' => 0)
),
array(
'OptIn' => array('name' => 'OptIn', 'type' => 'xsd:boolean', 'use' => 'required')
)
);
$soapserver->register('taxatie', // method name
array(), // input parameters
array('return' => 'tns:Taxatie'), // output parameters
'urn:thijs.test', // namespace
'urn:thijs.test#taxatie', // soapaction
'rpc', // style
'encoded', // use
'return something' // documentation
);
class taxatie
{
var $EmailAdres = null;
function taxatie()
{
$this->EmailAdres = new emailadres();
}
}
class emailadres
{
var $EmailAdres = 'email#domain.com';
var $OptIn = true;
}
function taxatie()
{
return new taxatie();
}
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$soapserver->service($HTTP_RAW_POST_DATA);
Thanks in advance

Why not simply return an array instead of using complex type?
e.g
$resultarray['taxatie'] = array(
'EmailAdres' => $row["EmailAdres"],
'tagcount' => $row["tagcount"],
'OptIn' => $row["OptIn"]
);

Related

How to send multi response using nusoap with soapval?

I am stuck in a situation where I am unable to send multi-response not array type using soapval(nusoap). I looking for a solution to make struct which accepts multi-request and send multi-response in nusoap. I saw a lot of examples where I only saw the response as an array this is the main issue I think so.
Let me show you my code
$server->wsdl->addComplexType(
'InitiateHolidayDataTransferResult',
'complexType',
'struct',
'sequence',
'',
array( 'HOLIDAY_COMPLEX_TYPE_RES' => array(
'name' => 'HOLIDAY_COMPLEX_TYPE_RES', 'type' => 'tns:HOLIDAY_COMPLEX_TYPE_RES' ,'minOccurs' => '0', 'maxOccurs' => 'unbounded' )
)
);
$server->wsdl->addComplexType(
'HOLIDAY_COMPLEX_TYPE_RES',
'complexType',
'struct',
'all',
'',
array( 'STATUS' => array('name' => 'STATUS','type' => 'xsd:string'),
'STATUS_DESC' => array('name' => 'STATUS_DESC','type' => 'xsd:string')
));
// Register the method to expose
$server->register('HOLIDAY_INFORMATION_DATA', // method name
array('name' => 'tns:HOLIDAY_COMPLEX_TYPE_REQ'), // input parameters
array('return' => 'tns:HOLIDAY_COMPLEX_TYPE_RES'), // output parameters
'urn:HOLIDAY_SERVER1', // namespace
'urn:HOLIDAY_SERVER1#HOLIDAY_INFORMATION_DATA', // soapaction
'document', // style
'encoded', // use
'Holiday Information Get Method' // documentation
);
// Define the method as a PHP function
function HOLIDAY_INFORMATION_DATA($mycomplextype) {
$holiday_data_success='';
// trying to send multi reponse in many ways see below
foreach($mycomplextype as $key=>$val){
//one way
$parm = array();
$parm[] = array('STATUS'=>"Success", "STATUS_DESC"=>"-Holiday Added");
$parm[] = array('STATUS'=>"Success", "STATUS_DESC"=>"-Holiday Added");
$parm[] = array('STATUS'=>"Success", "STATUS_DESC"=>"-Holiday Added");
$holiday_data_success = new soapval('return', 'tns:HOLIDAYRES_COMPLEX_TYPE_RES', $parm);
//second way
$arr = array("STATUS"=>"12345", "STATUS_DESC"=>"Test heading",);
$arr2 = array("STATUS"=>"12346", "STATUS_DESC"=>"Test heading");
$holiday_data_success = array($arr, $arr2);
// third way
$holiday_data_success=[];
$holiday_data_success[] = new soapval('return', 'tns:InitiateHolidayRESTransferResult', array('STATUS'=>"Success", "STATUS_DESC"=>$successtr."-Holiday Added"));
} // end foreach
return $holiday_data_success;
}
// but nothing is working
I am unable to send multi-response for each record that I insert in db

NuSoap, XML was empty, couldn't parse

First, I have to say that I am totally new to WSDL-based Web Services using NuSOAP. I am trying to return an array of friends list from my soap server. Please find below my code listings as is:
Server:
//File includes omitted
function getFriendList($test = ''){
$results = array();
$results[] = array('name' => 'XXXXA1', 'surname' => 'XXXXA2');
$results[] = array('name' => 'XXXXXB1', 'surname' => 'XXXXB2');
$results[] = array('name' => 'XXXXXC1', 'surname' => 'XXXXC2');
return $results;
}
//Create server instance
$server = new soap_server();
//Configure our WSDL
$server->configureWSDL('server', 'urn:server');
//Add our Complex Type data type since we want to return an array
$server->wsdl->addComplexType(
'Friend',
'complexType',
'struct',
'all',
'',
array(
'name' => array('name' => 'name', 'type' => 'xsd:string'),
'surname' => array('name' => 'surname', 'type' => 'xsd:string')
)
);
//Register our array as a response
$server->wsdl->addComplexType(
'FriendArray',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Friend[]')
),
'tns:Friend'
);
//Register the actual function that retuns the array but in the
//return n field specify the complex type of array we added onto the wsdl
$server->register('getFriendList',
array('test' => 'xsd:string'),
array('return' => 'tns:FriendArray'),
'urn:server',
'urn:server#getFriendList',
'document',
'encoded',
'Fetch a list of friends as an array. If you\'re not here sorry.'
);
//Serve the service
$server->service($HTTP_RAW_POST_DATA = (!isset($HTTP_RAW_POST_DATA)) ? $HTTP_RAW_POST_DATA : '');
With this, I hoped that I did everything correctly. I followed the standard procedure to make it work but in vein.
On the client side, I used the soap address url in wsdl and appended it with "?wsdl", so that it give me wsdl file dynamically.
example:
//Client call
$client = new nusoap_client("http://soapserver.dev/webroot/part_three/server.php?wsdl", true);
I have also taken labour of adding some whistles and bells on my client object as shown below:
$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = false;
This doesn't seem to help at all. I had experienced an issue earlier since my function accepts no parameter, where my service died because I didnt pass any parameter that speaks of inputs in my $client->call($name, $in, $out). I ended up providing an empty array and I got this error.
Could this be related to the rpc/document parameter types of the register function?
Even using the wsdl file which I saved from my server url, I got the same error.
Can somebody please help me out here? Please!

PHP, MYSQL Web Service with nusoap

I'm starting to learn about web services and I'm doing some tests.
I'm trying to write a service to pass all the data of a table into my client but i can't make it work.
The first one 'servei' works, but the second doesn't.
Any suggestions will be appreciated. Thanks
service.php
require 'lib/nusoap.php';
$server = new nusoap_server();
$server->configureWSDL("test" . "urn:test");
include 'functions.php';
$server->wsdl->addComplexType('servei', 'complexType', 'struct', 'all', '', array(
'preu_antic' => array('name' => 'preu_antic', 'type' => 'xsd:float'),
'preu_actual' => array('name' => 'preu_actual', 'type' => 'xsd:float'),
'descompte_servei' => array('name' => 'descompte_servei', 'type' => 'xsd:float'),
));
$server->wsdl->addComplexType('ServiceTypes', 'complexType', 'struct', 'all', '', array(
'id_tipus_servei' => array('name' => 'id_tipus_servei', 'type' => 'xsd:inter'),
'nom_tipus_servei' => array('name' => 'nom_tipus_servei', 'type' => 'xsd:string'),
'descripcio_tipus_servei' => array('name' => 'descripcio_tipus_servei', 'type' => 'xsd:string'),
));
$server->wsdl->addComplexType('ArrayOfServiceTypes', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:ServiceTypes[]')), 'tns:ServiceTypes');
$server->register('servei', array("id_servei" => 'xsd:inter'), array("return" => 'tns:servei'));
$server->register('getServiceTypes', array("idioma" => 'xsd:string'), array("return" => 'tns:ArrayOfServiceTypes'));
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
functions.php
function servei($id){
conexio();
$query_servei = mysql_query("SELECT * FROM servei WHERE id_servei = '$id'")or die(mysql_error());
$row = mysql_fetch_array($query_servei);
$preu_antic = $row['preu_antic'];
$preu_actual = $row['preu_actual'];
$descompte_servei = $row['descompte_servei'];
$servei = array('preu_antic'=>$preu_antic,'preu_actual'=>$preu_actual,'descompte_servei'=>$descompte_servei);
return $servei;
}
function getServiceTypes($idioma){
conexio();
$query = mysql_query("SELECT id_tipus_servei, nom_tipus_servei, descripcio_tipus_servei FROM idioma_tipus_servei WHERE id_idioma = '$idioma'") or die(mysql_error());
$n = 0;
while ($row = mysql_fetch_array($query)) {
$result[$n]['id_tipus_servei'] = $row['id_tipus_servei'];
$result[$n]['nom_tipus_servei'] = $row['nom_tipus_servei'];
$result[$n]['descripcio_tipus_servei'] = $row['descripcio_tipus_servei'];
$n++;
}
return $result;
}
?>
client.php
<?php
require 'lib/nusoap.php';
include 'functions.php';
$client = new nusoap_client("http://192.168.8.155:8090/webservice/service.php?wsdl");
//$id=1;
// $servei = $client -> call('servei',array("id_servei"=>"$id"));
// print_r($servei);
$idioma ='ca';
$servicetypes = $client -> call('getServiceTypes',array("idioma"=>"$idioma"));
print_r($servicetypes);
?>
OK, I handled it.
If you are using PHP 5.4 you have to comment line 6132 in nusoap.php.
Couldn't this be fixed if we were to change it from:
$this->debug("serializing array element: $k, $v of type: $typeDef[arrayType]");
To:
$this->debug("serializing array element: $k, $v of type: " . $typeDef['arrayType'] );

Pass multidimensional array to soap complexType in PHP

I am pulling some emails from my mail server. There is a function should pull these emails and return a multidimensional array. I use this array in client web server to do the job for me. I don't know how to pass this array to the soap complexType. I wrote the following code:
$server->wsdl->addComplexType(
'MailTicket',
'complexType',
'struct',
'all',
'',
array(
'attachment' => array('name' => 'attachment', 'type' => 'xsd:string'),
'body' => array('name' => 'body', 'type' => 'xsd:string'),
'accountID' => array('name' => 'accountID', 'type' => 'xsd:string')
)
);
$server->wsdl->addComplexType(
'MailTicketReturn',
'complexType',
'struct',
'all',
'',
array(
'Done' => array('name' => 'result', 'type' => 'xsd:string')
)
);
// Register the method to expose
$server->register('createMailTicket', // method name
array('mailTicketData' => 'tns:MailTicket'), // input parameters
array('return' => 'tns:MailTicketReturn'), // output parameters
'urn:eticketing', // namespace
'urn:eticketing#createMailTicket', // soapaction
'rpc', // style
'encoded', // use
'create a ticket by mail' // documentation
);
and on the client, I wrote:
require_once('nusoap.php');
$wsdlURL="http://127.0.0.1/eticket/ETKWS.php?wsdl";
$client = new nusoap_client($wsdlURL,true);
$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = false;
$finalArray=Array
(
[attachment] => Array
(
[0] => Array
(
[0] => file1
[1] => file2
)
[1] => Array
(
[0] => file1x
)
)
[body]=>Array
(
[0] => some text
[1] => some other text
)
[accountID] => Array
(
[0] => 5464654
[1] => 4654664
)
)
if(is_array($finalArray)) // creat new tickets
{
$result=$client->call('createMailTicket',$finalArray);
}
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
exit();
}
I got this error:
Constructor error
XML error parsing SOAP payload on line 1: Not well-formed (invalid token)
NuSOAP support return multidimensional array (xsd:Array)
$server= new nusoap_server();
$namespace = "http://localhost/webservice/";
// create a new soap server
$server = new nusoap_server();
// configure our WSDL
$server->configureWSDL("WebServices212");
// set our namespace
$server->wsdl->schemaTargetNamespace = $namespace;
$server->register(
// method name:
'test',
// parameter list:
array('id'=>'xsd:int'),
// return value(array()):
array('return'=>'xsd:Array'),
// namespace:
$namespace,
// soapaction: (use default)
false,
// style: rpc or document
'rpc',
// use: encoded or literal
'encoded',
// description: documentation for the method
'documentation');
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA'])
? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
Client
client=new nusoap_client("http://localhost/webservice /webservices.php?wsdl");
$client->setCredentials("webadmin","****");
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
$result = $client->call('test', array('id' => '1'));
print_r($result);
if you consume the webservices from PHP no problem, but in other languages there are compatibility problems

Nusoap - fix empty xmlns attribute for the response

i have created a simple web service using Php Nusoap. its working correctly but the only thing missing is to add the default xmlns attribute to the response tag.
Here is the copy of Response :
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<LoginResponse xmlns="">
<LoginResult>
<register>
<customer>d2ff3b88d34705e01d150c21fa7bde07</customer>
</register>
</LoginResult>
</LoginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Here is the code :
<?php
require_once ('nusoap.php');
// set namespace
$ns = 'mynamspace';
// set up soap server
$server = new soap_server ();
$server->configureWSDL ( 'testservice', $ns);
$server->wsdl->schemaTargetNamespace = $ns;
// define new user data type
// define results
$server->wsdl->addComplexType ( 'customer', 'complexType', 'struct', '', '', array ('customer' => array ('name' => 'customer', 'type' => 'xsd:string' ) ) );
$server->wsdl->addComplexType ( 'register', 'complexType', 'struct', '', '', array ('register' => array ('name' => 'register', 'type' => 'tns:customer' ) ) );
$server->wsdl->addComplexType ( 'LoginResult', 'complexType', 'struct', '', '', array ('LoginResult' => array ('name' => 'LoginResult', 'type' => 'tns:register' ) ) );
// register Login function
$server->register ( 'Login', // method name
array ('username' => 'xsd:string', 'password' => 'xsd:string' ), // input parameters
array ('LoginResult' => 'tns:register' ), // output parameters
'urn:mynamespace', // namespace
'urn:mynamespaceAction', // soapaction
'document', // style
'literal', // use
'Login service for testing' ); // documentation
function Login($username, $password) {
if (isset ( $username ) && isset ( $password )) {
$hash = md5 ( $username );
return array ('LoginResult' => array ('register' => array ('customer' => $hash ) ));
}
}
// 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 );
?>
Any help is greatly appreciated.
I don't claim this is particularly elegant, but worked in my case:
I went into the nusoap.php and commented this line (line 5925 for me):
//$elementNS = " xmlns=\"\"";
directly after this:
if ($unqualified && $use == 'literal') {

Categories