从 php 调用 SOAP Web 服务时收到 Web 方法参数的空值

receiving null value of parameters of web method when calling SOAP Web Service from php

我有一个用 Java 编写的 Soap Web 服务。

我从 php 调用此 Web 服务并将参数传递给 Web 方法,但在 Web 方法中得到 null。

我的网络服务如下:

@SOAPBinding(style = Style.RPC)
@WebService(serviceName = "Test")
public class Test {

        @WebMethod(operationName = "hello")
        public String hello(@WebParam(name = "name1") String txt1, @WebParam(name = "name2") String txt2) {

            System.out.println("Request : Hello " + txt1+" , "+txt2);
            return "Response : Hello " + txt1 + " ! " + txt2;
    }
}

而WSDL的消息部分如下:

<message name="hello">
<part name="name1" type="xsd:string"/>
<part name="name2" type="xsd:string"/>
</message>
<message name="helloResponse">
<part name="return" type="xsd:string"/>
</message>

并从 php 调用网络服务如下:

$client = new SoapClient("http://localhost:8081/androidWebServiceTest/Test?wsdl"); // soap webservice call

$functions = $client->__getFunctions();    // getting list of web methods

$types = $client->__getTypes();    // getting types

$response = $client->hello(array("parameters" => array("name1" => "test1", "name2" => "test2")));    // calling web method hello

// dumping $functions, $types and $response
var_dump($functions);    
var_dump($types);
var_dump($response);

这个调用的输出如下:

D:\wamp64\www\WebService\netbeans\Monitor2Test\test.php:49:
array (size=1)
  0 => string 'helloResponse hello(hello $parameters)' (length=38)
D:\wamp64\www\WebService\netbeans\Monitor2Test\test.php:51:
array (size=2)
  0 => string 'struct hello {
 string name;
}' (length=30)
  1 => string 'struct helloResponse {
 string return;
}' (length=40)
D:\wamp64\www\WebService\netbeans\Monitor2Test\test.php:53:
object(stdClass)[3]
  public 'return' => string 'Response : Hello null ! null' (length=19)

而服务器日志中的输出如下:

Request : Hello null , null

我尝试了不同的解决方案,但对我来说没有任何效果,仍然无法将参数发送到网络方法,总是在网络方法中得到 null。

试试这个:

<?php
// ...    
$soapClient->hello([
    'name1' => 'test1', 
    'name2' => 'test2'
]);
// or 
$soapClient->__soapCall('hello', [
    'parameters' => [
        'name1' => 'test1', 
        'name2' => 'test2'
    ]
]);

我的头撞了两天后找到了解决办法。问题是我使用 Web 服务的 PHP 脚本正在使用缓存的 soap WSDL。在我的 PHP 脚本的开头添加以下行后问题就解决了。

解决方案:[在 PHP 代码的开头添加此行]

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