服务器:
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 服务器。