服务器:
import soaplib
from soaplib.core.service import rpc, DefinitionBase
from soaplib.core.model.primitive import String, Integer
from soaplib.core.server import wsgi
from soaplib.core.model.clazz import Array
class HelloWorldService(DefinitionBase):
@soap(String,Integer,_returns=Array(String))
def say_hello(self,name,times):
results = []
for i in range(0,times):
results.append('Hello, %s'%name)
return results
if __name__=='__main__':
try:
from wsgiref.simple_server import make_server
soap_application = soaplib.core.Application([HelloWorldService], 'tns')
wsgi_application = wsgi.Application(soap_application)
server = make_server('173.252.236.136', 7789, wsgi_application)
server.serve_forever()
except ImportError:
print "Error: example server code requires Python >= 2.5"
PHP:
$client=new SoapClient("http://173.252.236.136:7789/?wsdl");
try{
ini_set('default_socket_timeout', 5);
var_dump($client->say_hello("Dave", 5));
echo("<br />");
//print_r($client->add(1,2));
}catch(Exception $e){
echo $e->__toString();
ini_restore('default_socket_timeout');
}
当我运行我的PHP代码时,它会报告以下信息:
SoapFault exception: [senv:Server] range() integer end argument expected, got NoneType. in E:\web\webservice\client.php:6 Stack trace: #0 E:\web\webservice\client.php(6): SoapClient->__call('say_hello', Array) #1 E:\web\webservice\client.php(6): SoapClient->say_hello('Dave', 5) #2 {main}
但它可以使用Python的客户端:
from suds.client import Client
hello_client = Client('http://173.252.236.136:7789/?wsdl')
result = hello_client.service.say_hello("Dave", 5)
print result
我发现我无法使用PHP客户端将参数name
和times
发送到Python服务器。
答案 0 :(得分:1)
我遇到了这个问题。如果要将参数传递给python webservice。你只需要传递一个数组作为参数。就像这样:
的var_dump($客户端 - &GT; say_hello(阵列(&#34;名称&#34; =&GT;&#34; Dave和#34;&#34;倍&#34 =大于5)));
答案 1 :(得分:0)
我想问题是,soaplib
包装参数和Soap-structs中的返回值。如果你看一下输出:
var_dump($client->__getFunctions());
var_dump($client->__getTypes());
所以解决方案就是提供结构:
class SayHelloStruct {
function __construct($name, $times) {
$this->name = $name;
$this->times = $times;
}
}
$struct = new SayHelloStruct("Dave", 5);
// here "say_hello" is not the method name but the name of the struct
$soapstruct = new SoapVar($struct, SOAP_ENC_OBJECT, "say_hello");
$param = new SoapParam($soapstruct, "say_hello");
var_dump($client->say_hello($param));
您可以通过将哈希数组转换为对象来缩短这一点:
$struct = (object)array("name" => "Dave", "times" => 5);
// here "say_hello" is not the method name but the name of the struct
$soapstruct = new SoapVar($struct, SOAP_ENC_OBJECT, "say_hello");
$param = new SoapParam($soapstruct, "say_hello");
var_dump($client->say_hello($param));
SoapParam
- 最终不需要。你可以省略它而不会破坏它。
我真的不知道是否有更好的解决方案,比如服务器或客户端的某种标志。