How can I pass in an array as a value into a PHP s

2019-05-26 07:18发布

How can I pass in an array as a value into a PHP soapclient request?

I have a soapclient instantiated and connected already. I then try to make a call to a webservice method that expects 3 parameters (string, string, hashmap).

Here is what I expected to work below. But when viewing the xml output, the params node is empty.

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

The soap body xml ends up like this (note the empty params element):

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

2条回答
Viruses.
2楼-- · 2019-05-26 07:50

For JAX-WS webservices it may be a problem with the hashmap input parameter. The xsd schema generated seems to be incorrect for hashmaps. Placing the map in a wrapper object causes JAX-WS to output the correct 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
}

Then the php code will succeed. I found I didn't need SoapVar or SoapParam and could not get either of those methods to work without the MapWrapper.

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

Here is the correct xsd generated with the wrapper

<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>

Here is the incorrect schema generated by JAX-WS with just the hashmap

<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>

One last note. Wrapping HashMap<String, String> worked with this solution, but HashMap<String, Object> did not. The Object gets mapped to xsd:anyType which comes into the java webservice as a xsd schema object rather than just Object.

查看更多
Fickle 薄情
3楼-- · 2019-05-26 07:56

Depending on the webservice definition, the hashmap parameter might need to have a specific structure/encoding that can not be directly created from an array. You might want to check the WSDL on that, and take a look into the SoapVar and the SoapParam classes for more options on Soap parameter construction.

查看更多
登录 后发表回答