views:

108

answers:

2

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' => '[email protected]', '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>

Please help!

A: 

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.

Henrik Opel
A: 

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.

gabe