Nusoap - fix empty xmlns attribute for the response - php

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') {

Related

PHP Soap And .Net Dataset

I got an issue here. I am trying make a request to a Web Appi: http://www.speedex.gr/getvoutrans/getvoutrans.asmx?WSDL
And I am sending a request to insertPodData();
I am using PHP and SOAP.
I am succesfull at connecting and giving the correct credentials. However I am not able to send a Dataset (cause I do not know the right way), so i get an empty dataset.
Datasets are for .NET lang. So it is kind of tricky with the php.
I tried already to send it as an array, i still get an empty result.
Here are some coding.
PHP:
$dataset = array(
'schema' => array(
'Enter_Branch_Id' => $speedex_branch_id,
'SND_Customer_Id' => $speedex_cust_id,
'SND_Agreement_Id' => $speedex_appi_key,
'RCV_Name' => 'Test',
'RCV_Addre1' => 'Test Adress',
'RCV_Zip_Code' => '54636',
'RCV_City' => 'Thessaloniki',
'RCV_Country' => 'Greece',
'RCV_Tel1' => '*******',
'Voucher_Weight' => '0',
),
'any' => ''
);
try {
$soap = new SoapClient("http://www.speedex.gr/getvoutrans/getvoutrans.asmx?WSDL",array('trace' => true));
$oAuthResult = $soap->insertPodData(
array(
'username' => $speedex_usrn,
'password' => $speedex_psw,
'VoucherTable' => $dataset,
'_tableFlag' => 3
)
);
$resultVoucher = $oAuthResult;
print_r($resultVoucher);
echo '<br>';
echo "REQUEST:\n" . htmlentities($soap->__getLastRequest()) . "\n";
die();
}catch(SoapFault $fault) {
die('<h1>Ooooops something is broken. Refresh or contact module creator </h1><br>'.$fault);
}
This is returning this result
RESULT: stdClass Object ( [insertPodDataResult] => 1 [newVoucherTable] => stdClass Object ( [schema] => [any] => ) )
REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:insertPodData>
<ns1:username>****</ns1:username>
<ns1:password>****</ns1:password>
<ns1:VoucherTable>********************TestTest Adress54636ThessalonikiGreece********</ns1:VoucherTable>
<ns1:_tableFlag>3</ns1:_tableFlag>
</ns1:insertPodData>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As you can observe the Dataset is not created and all the values are passed with out a reference.
Any ideas clues? Thanks in advance!

how to deal with array ComplexType in NuSoap?

currently i am using PHP Version 5.4.16, and NuSoap 0.95, i have try this example :
here
which shown the server code like this ;
$server->wsdl->addComplexType(
'Chapter',
'complexType',
'struct',
'all',
'',
array(
'title' => array('name'=>'title','type'=>'xsd:string'),
'page' => array('name'=>'page','type'=>'xsd:int')
)
);
$server->wsdl->addComplexType(
'ChapterArray',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Chapter[]')
),
'tns:Chapter'
);
$server->wsdl->addComplexType(
'Book',
'complexType',
'struct',
'all',
'',
array(
'author' => array('name'=>'author','type'=>'xsd:string'),
'title' => array('name'=>'title','type'=>'xsd:string'),
'numpages' => array('name'=>'numpages','type'=>'xsd:int'),
'toc' => array('name'=>'toc','type'=>'tns:ChapterArray')
)
);
$server->register(
'getBook',
array('title'=>'xsd:string'),
array('return'=>'tns:Book'),
$NAMESPACE);
function getBook($title) {
// Create TOC
$toc = array();
$toc[] = array('title' => 'Chapter One', 'page' => 1);
$toc[] = array('title' => 'Chapter Two', 'page' => 30);
// Create book
$book = array(
'author' => "Jack London",
'title' => $title,
'numpages' => 42,
'toc' => $toc);
return $book;
}
but got error returned at the SOAP Response, i am testing it using SopUI. i got an error like this :
<br />
<b>Notice</b>: Array to string conversion in <b>E:\xampp\htdocs\myweb\lib\nusoap.php</b> on line <b>6132</b><br />
<br />
<b>Notice</b>: Array to string conversion in <b>E:\xampp\htdocs\myweb\lib\nusoap.php</b> on line <b>6132</b><br />
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope 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><ns1:getBookResponse xmlns:ns1="http://localhost/myweb"><return><author>Jack London</author><title>This is my book</title><numpages>42</numpages><toc><item><title>Chapter One</title><page>1</page></item><item><title>Chapter Two</title><page>30</page></item></toc></return></ns1:getBookResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
how can i fix this error? can somebody help me to solve this error please... many thanks in advance...
In nusoap > lib > nusoap.php, comment line 6132:
////$this->debug("serializing array element: $k, $v of type: $typeDef[arrayType]");
$server->wsdl->addComplexType(
'Book',
'complexType',
'struct',
'all',
'',
array(
'author' => array('name1'=>'author','type'=>'xsd:string'),
'title' => array('name2'=>'title','type'=>'xsd:string'),
'numpages' => array('name3'=>'numpages','type'=>'xsd:int'),
'toc' => array('name4'=>'toc','type'=>'tns:ChapterArray')
)
);
$server->register(
'getBook',
array('title'=>'xsd:string'),
array('return'=>'xsd:array'),
$NAMESPACE);
A neater solution than commenting the line 6132 in nusoap > lib > nusoap.php would be to check if $v is an array and convert it to string:
current line 6132:
$this->debug("serializing array element: $k, $v of type: $typeDef[arrayType]");
change to:
$this->debug("serializing array element: $k, " . ( is_array($v) ? join(',', $v) : $v ) . " of type: $typeDef[arrayType]");

Returning multiple values via SOAP in PHP

tl;dr: sending multiple strings as a response to SOAP request.
I am new to SOAP. I have written a simple web service which serves request via SOAP. As I wanted to implement this in PHP, I have used NuSOAP library. The specification given to me for the SOAP API design is as follows.
REQUEST FORMAT:
<?xml version="1.0" encoding="utf-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://www.sandeepraju.in/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soapenv:Body> 
<q0:getData> 
<token>String</token> 
</q0:getData> 
</soapenv:Body> 
</soapenv:Envelope>
EXAMPLE / SAMPLE RESPONSE:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getDataResponse xmlns:ns2="http://www.sandeepraju.in/">
<return>
<Data>
<animal>cat</animal>
<phone>225</phone>
<code>FYI</code>
</Data>
The PHP code I have written for the above specification is as follows.
require_once("../nusoap_old/lib/nusoap.php");
// Definition of getData operation
function getData($token) {
if($token == "somestring") {
return array("animal" => "cat", "phone" => "225", "code" => "FYI");
}
else {
return array("animal" => "null", "phone" => "null", "code" => "null");
}
}
// Creating SOAP server Object
$server = new soap_server();
// Setup WSDL
$server->configureWSDL('catnews', 'urn:catinfo');
$server->wsdl->addComplexType('return_array_php',
'complexType',
'struct',
'all',
'',
array(
'animal' => array('animal' => 'animal', 'type' => 'xsd:string'),
'phone' => array('phone' => 'phone', 'type' => 'xsd:string'),
'code' => array('code' => 'code', 'type' => 'xsd:string')
)
);
// Register the getData operation
$server->register("getData",
array('token' => 'xsd:string'),
array('return' => 'tns:return_array_php'),
'urn:catinfo',
'urn:catinfo#getData');
// Checking POST request headers
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : "";
$server->service($HTTP_RAW_POST_DATA);
Here, I think I should NOT return a PHP array. But I am not sure what I should return according the specification. Can anyone help me with this. Or returning an array is correct?
You need to add another complex type for an array consisting of your data.
Like this:
$server->wsdl->addComplexType(
'dataArray', // MySoapObjectArray
'complexType', 'array', '', 'SOAP-ENC:Array',
array(),
array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:return_array_php[]')), 'tns:return_array_php'
);
Register the new datatype to be the return-value of the function.
$server->register(
'getData',
array('Datum'=>'xsd:token'),
array('return'=>'tns:dataArray'),
$namespace,
false,
'rpc',
'encoded',
'description'
);
Then your function needs to set the single parts of the array.
function GetData($token)
{
if($token == "somestring") {
$result[0] = array(
"animal" => "cat",
"phone" => "225",
"code" => "FYI"
);
$result[1] = array(
"animal" => "dog",
"phone" => "552",
"code" => "IFY"
);
} else {
$result = null;
}
return $result;
}
The response of this service called with the string "somestring" will be:
<ns1:getDataResponse xmlns:ns1="http://localhost/status/status.php">
<return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:return_array_php[2]">
<item xsi:type="tns:return_array_php">
<animal xsi:type="xsd:string">cat</animal>
<phone xsi:type="xsd:string">225</phone>
<code xsi:type="xsd:string">FYI</code>
</item>
<item xsi:type="tns:return_array_php">
<animal xsi:type="xsd:string">dog</animal>
<phone xsi:type="xsd:string">552</phone>
<code xsi:type="xsd:string">IFY</code>
</item>
</return>
</ns1:getDataResponse>
That matches your specifications.

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

How can i set attributes with NuSoap in 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"]
);

Categories