使用PHP调用Python Web服务

时间:2012-02-08 08:50:33

标签: php python soap

Python Web服务代码:

import web 
from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers import primitive as soap_types

urls = ("/hello", "HelloService",
        "/hello.wsdl", "HelloService",
        )
render = web.template.Template("$def with (var)\n$:var")



class SoapService(SimpleWSGISoapApp):
    """Class for webservice """
    @soapmethod(soap_types.String,_returns=soap_types.String)
    def hello(self,message):
        """ Method for webservice"""
        return "Hello world "+message



class HelloService(SoapService):
    """Class for web.py """
    def start_response(self,status, headers):
        web.ctx.status = status
        for header, value in headers:
            web.header(header, value)


    def GET(self):
        response = super(SimpleWSGISoapApp, self).__call__(web.ctx.environ, self.start_response)
        return render("\n".join(response))


    def POST(self):
        response = super(SimpleWSGISoapApp, self).__call__(web.ctx.environ, self.start_response)
        return render("\n".join(response))

app=web.application(urls, globals())

if __name__ == "__main__":
    app.run()

PHP代码:

<?php

    @ini_set("soap.wsdl_cache_enabled", "0");


    $client = new SoapClient('http://localhost:8080/hello.wsdl');

    echo("<pre>");
    var_dump($client->__getFunctions());
    echo("</pre>");
    $params = array('World');


    try {
        print_r($client->__soapCall('hello', $params));

    } catch (SoapFault $exception) {
        echo $exception;
    }
?>

当我运行我的php代码时,它会报告下面的信息

  

SoapFault异常:[helloFault] hello()只需要2个参数(1   给出)在C:\ website \ cosmetics \ src \ test02.php:15堆栈跟踪:#0   C:\网站\化妆品\ SRC \ test02.php(15):   SoapClient-&gt; __ soapCall('hello',数组)#1 {main}

如何解决这个问题?感谢。

1 个答案:

答案 0 :(得分:1)

不确定SoapClient究竟是什么,但它看起来只需要另一个参数。文件说了什么?