How to set a namespace with NuSOAP (client side)
-
01-07-2021 - |
Вопрос
I asked a question like this some months ago but the problem was that the WebService was not working right. Now it is working perfectly and I'm still having problems making a simple request.
First of all, I tried it with http://www.validwsdl.com/
The WS is http://amibcertifica.amib.com.mx:9090/axis2/services/JpaWebServicesAmib?wsdl
You can try it yourselves. It works just fine with that website.
Now I'm trying to make a request with NuSOAP and I get the this error: namespace mismatch require http://ws.mobius.amib found http://tempuri.org
You can check the whole error here: http://dev.etic.com.mx/bmv/test.php
My code is the following:
<?php
require_once('nusoap/lib/nusoap.php');
$url = "http://amibcertifica.amib.com.mx:9090/axis2/services/JpaWebServicesAmib?wsdl";
try
{
$client = new nusoap_client($url);
$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
}
$result = $client->call('findAllComprobanteOperacion');
}
catch (SoapFault $e)
{
echo 'Error0'.$e->getMessage() . "\n";
}
echo '<pre>';print_r( $result );
echo $client->debug_str;
?>
NuSOAP version: $Id: nusoap.php,v 1.123 2010/04/26 20:15:08 snichol Exp $
I've been looking all around the web on how to do this and I'm completely clueless, so any help is really appreciated. Thanks in advance.
Решение 2
It was simpler than I thought. I just had to set the second parameter of nusoap_client to TRUE (because it's FALSE by default). So, it's $client = new nusoap_client($url, TRUE);
That's it.
Thanks anyway.
Другие советы
You set the namespace by declaring it as an argument. By default the namespace is set to "http://tempuri.org" (some soap servers require an additional forward slash like 'http://tempuri.org/'). So when you use the call function, you can set the namespace in the third argument like this:
$result = $client->call('findAllComprobanteOperacion', null, 'http://tempuri.org/');
You can also declare your arguments and set the SoapAction, the fourth argument, like this:
$result = $client->call('findAllComprobanteOperacion', array('Argument1' => 'value1', 'Argument2' => 'value2'), 'http://tempuri.org/', 'SoapActionHere');
By default, NuSoap also forces a random number between 1000 and 9999 as the prefix (variable name) for the namespace. This can also be disliked by a server. There's no built in way to actually correct this. I simply edited nusoap.php to fix this issue. On line 7431 (or thereabouts), you'll find:
$this->debug("wrapping RPC request with encoded method element");
if ($namespace) {
// http://www.ws-i.org/Profiles/BasicProfile-1.1-2004-08-24.html R2735 says rpc/literal accessor elements should not be in a namespace
$payload = "<$nsPrefix:$operation xmlns:$nsPrefix=\"$namespace\">" .
$payload . "</$nsPrefix:$operation>";
} else {
$payload = "<$operation>" . $payload . "</$operation>";
}
change it to:
if ($namespace) {
$payload = "<$operation xmlns=\"$namespace\">" .
$payload . "</$operation>";
} else {
$payload = "<$operation>" .
$payload . "</$operation>";
}