I try to create a nusoap web service (I use xampp and .net beans) and connect to it using a C# app (use visual studio). but I encounter this error:
{"Server returned an invalid SOAP Fault. Please see InnerException for more details."}
the inner error is:
{"Element 'faultstring' with namespace name '' was not found. Line 6, position 126."}
As this source suggested I changed properties order for function serialize()
in class.soap_fault.php
file but it still gives the same error.
I tried restarting the computer so I now it is not a catch problem.
Also the only other change I made in nusoap library was changing var $soap_defencoding = 'ISO-8859-1';
to var $soap_defencoding = 'UTF-8';
in nusoap.php
file to solve encoding problem.
in C# I have the following code:
SR1.demoPortTypeClient client = new SR1.demoPortTypeClient();
var result = client.gettext("hello");
MessageBox.Show("*" + result + "*");
and in php:
<?php
require 'lib/nusoap.php';
$server=new nusoap_server();
$server->configureWSDL("demo");
$namespace = "demo";
$server->wsdl->schemaTargetNamespace = $namespace;
$server->register(
"gettext",//name of function
array("txt"=>'xsd:string'),//inputs
array("return"=>'xsd:string'),//outputs
$namespace,// namespace
false,// soapaction (use default)
'rpc',// style: rpc or document
'encoded',// use: encoded or literal
'Return same text'// description for method
);
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
where gettext
function is:
<?php
function gettext($txt)
{
return $txt;
}
current serialize()
function is:
function serialize(){
$ns_string = '';
foreach($this->namespaces as $k => $v){
$ns_string .= "\n xmlns:$k=\"$v\"";
}
$return_msg =
'<?xml version="1.0" encoding="'.$this->soap_defencoding.'"?>'.
'<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"'.$ns_string.">\n".
'<SOAP-ENV:Body>'.
'<SOAP-ENV:Fault>'.
$this->serialize_val( $this->faultcode, 'faultcode' ) .
$this->serialize_val( $this->faultstring, 'faultstring' ) .
$this->serialize_val( $this->faultactor, 'faultactor' ) .
$this->serialize_val( $this->faultdetail, 'detail' ) .
'</SOAP-ENV:Fault>'.
'</SOAP-ENV:Body>'.
'</SOAP-ENV:Envelope>';
return $return_msg;
}
I expected changing order of properties solves the problem, however I still receive the same error. I searched a lot but couldn't find any other solve for the error.
For reason I do not know, there is two definition for function serialize()
with same body in two different file. One in class.soap_fault.php
and another in nusoap.php
and you need to change the method body in the second one (nusoap.php
). I do not know is there any condition you need to change other one or not.
For other ones who may reach this post I add some problems you may encounter and their solve:
Exception :
Encodes were different (windows uses 'utf-8' and this uses 'ISO-8859-1')
Reason: Encoding type are different in server and client.
Solve: In nusoap.php
file changing var $soap_defencoding = 'ISO-8859-1';
to var $soap_defencoding = 'UTF-8';
See: this source
Exception :
Server returned an invalid SOAP Fault. Please see InnerException for more details
Inner:
Element ‘faultstring’ with namespace name ” was not found. Line 6, position 126
Reason: Difference in order of fault properties
Solve: in nusoap.php
change function serialize()
to following:
function serialize() {
$ns_string = '';
foreach( $this->namespaces as $k => $v ) {
$ns_string .= "\n xmlns:$k=\"$v\"";
}
$return_msg =
'<?xml version="1.0" encoding="' . $this->soap_defencoding . '"?>' .
'<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"' . $ns_string . ">\n" .
'<SOAP-ENV:Body>' .
'<SOAP-ENV:Fault>' .
$this->serialize_val( $this->faultcode, 'faultcode' ) .
$this->serialize_val( $this->faultstring, 'faultstring' ) .
$this->serialize_val( $this->faultactor, 'faultactor' ) .
$this->serialize_val( $this->faultdetail, 'detail' ) .
'</SOAP-ENV:Fault>' .
'</SOAP-ENV:Body>' .
'</SOAP-ENV:Envelope>';
return $return_msg;
}
See: this source
Exception:
{"error in msg parsing:\nxml was empty, didn't parse!"}
Reason: The following php function is not valid in current version:
solve: use following code for php version 5.6.0 to before 7.0.0
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
$server->service($POST_DATA);
and for version 7.0.0 and later use:
@$server->service(file_get_contents("php://input"));
See: This source
Exception:
{"method ''('function name here'('function name here') not defined in service('' '')"}
or simply:
method '' not defined in service.
Reason: When using C# to connecting to the nusoap
web server. It is disable to find methods defined in other files/classes.
Solve: define all method in the same file as you write web service
See: This source
Note: If you know a way to overcome this problem and let define methods in other files/classes (= make cleaner code) please comment it.
Exception:
{"Error in deserializing body of reply message for operation 'method name'."}
Reason: May have different reasons but in my case occure when try to use arrays as property for returned structure. C# will see array of string as string.
Solve: I solved it by using basic types.
Note: if you know better way please comment it