1

在过去的两天里,我一直在无助地尝试使用 NuSoap 在 PHP 中创建 Web 服务。然而,在无休止地筛选各种教程和指南之后,我仍然被10 行拒绝工作的代码所困扰。

服务器 :

// Pull in the NuSOAP code
require_once('./lib/nusoap.php');

// Create the server instance
$server = new soap_server;

// Initialize WSDL support
$server->configureWSDL('hellowsdl','http://mysite.com');

// Register the method to expose
$server->register('hello',
    array("params"=>"xsd:string"),
    array("result"=>"xsd:string"),
    'http://mysite.com'
);

// Define the method as a PHP function
function hello($name) {
        return 'Hello, ' . $name;
}

// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

和客户:

// Pull in the NuSOAP code
require_once('./lib/nusoap.php');

// Create the client instance
$client = new nusoap_client('http://localhost/nusoap/server.php?wsdl',true);

// Call the SOAP method
$result = $client->call('hello',array("name"=>"Scott"));

var_dump($result);

给我一个布尔值(假)

我哪里错了???

4

1 回答 1

1

当你注册方法时,你调用的是参数'params'而不是'name',代码应该是:

$server->register('hello',
array("name"=>"xsd:string"),
array("result"=>"xsd:string"),
'http://mysite.com'
);

不过,您不应该弄错,当我运行带有错误的代码时,我仍然得到

string(12) "Hello, "

(空的 $name 变量)。转到http://localhost/nusoap/server.php?wsdl并确保您可以从您正在执行客户端的同一台机器上看到 WSDL。

于 2010-12-07T01:29:47.887 回答