2

如何将数组作为值传递给 PHP soapclient 请求?

我已经实例化并连接了一个soapclient。然后我尝试调用一个需要 3 个参数(字符串、字符串、哈希图)的 web 服务方法。

这是我期望在下面工作的。但是在查看 xml 输出时,params 节点是空的。

soapclient->doSomething(array('id' => 'blah', 'page' => 'blah', 'params' => array('email' => 'test@test.com', 'password' => 'password', 'blah' => 'blah')));

肥皂正文 xml 最终是这样的(注意空的 params 元素):

<SOAP-ENV:Body><ns1:doSomething>
<id>blah</id>
<page>blah</page>
<params/>
</ns1:register></SOAP-ENV:Body>
4

2 回答 2

3

对于 JAX-WS Web 服务,可能是 hashmap 输入参数有问题。生成的 xsd 模式似乎对于哈希图不正确。将映射放置在包装器对象中会导致 JAX-WS 输出正确的 xsd。

public class MapWrapper {
    public HashMap<String, String> map;
}


// in your web service class
@WebMethod(operationName = "doSomething")
public SomeResponseObject doSomething(
        @WebParam(name = "id") String id,
        @WebParam(name = "page") String page,
        @WebParam(name = "params") MapWrapper params {
    // body of method
}

然后php代码就会成功。我发现我不需要 SoapVar 或 SoapParam,而且如果没有 MapWrapper,这些方法中的任何一个都无法工作。

$entry1['key'] = 'somekey';
$entry1['value'] = 1;
$params['map'] = array($entry1);
soapclient->doSomething(array('id' => 'blah', 'page' => 'blah', 
    'params' => $params));

这是使用包装器生成的正确 xsd

<xs:complexType name="mapWrapper">
  <xs:sequence>
    <xs:element name="map">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="key" minOccurs="0" type="xs:string"/>
                <xs:element name="value" minOccurs="0" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

这是 JAX-WS 仅使用哈希图生成的不正确模式

<xs:complexType name="hashMap">
  <xs:complexContent>
    <xs:extension base="tns:abstractMap">
      <xs:sequence/>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
<xs:complexType name="abstractMap" abstract="true">
  <xs:sequence/>
</xs:complexType>

最后一点。包装 HashMap<String, String> 与此解决方案一起使用,但 HashMap<String, Object> 没有。对象被映射到 xsd:anyType,它作为 xsd 模式对象而不仅仅是对象进入 java web 服务。

于 2010-10-13T20:33:13.837 回答
0

根据 web 服务定义,hashmap 参数可能需要具有无法直接从数组创建的特定结构/编码。您可能需要检查 WSDL,并查看SoapVarSoapParam类以获取有关 Soap 参数构造的更多选项。

于 2010-03-03T12:06:26.787 回答