如何在php中为soapclient构建KeyValuePair

In my case of SoapClient, the request XML should look like this:

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
<SOAP-ENV:Body>
    <SoapFunction xmlns="http://services.***/">
        <prop1>value1</prop1>
        <prop2>value2</prop2>
        <prop3>
            <KeyValuePair>
                <Key>string</Key>
                <Value>string</Value>
            </KeyValuePair>
            <KeyValuePair>
                <Key>string</Key>
                <Value>string</Value>
            </KeyValuePair>
        </prop3>
    </SoapFunction>
</SOAP-ENV:Body>
</sSOAP-ENV:Envelope>

I could build prop1 and prop2 correctly by the following code:

$parameters = array(
    'prop1' => value1,
    'prop2' => value2
);
$request = array($parameters);
$client->__soapCall('SoapFunction', $request);

But how could i build property prop3, especially construct type KeyValuePair which is defined in the WSDL file

According the definition in WSDL file

<s:complexType name="KeyValuePair">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="Key" type="s:string" />
        <s:element minOccurs="0" maxOccurs="1" name="Value" type="s:string" />
    </s:sequence>
</s:complexType>

I created a new class KeyValuePair which has the property 'Key' and 'Value'. Then i could use a SoapVar to be the prop3 like this

$kvp1= new SoapVar(new KeyValuePair('key1', 'value1'), XSD_ANYTYPE, 'KeyValuePair');
$kvp2= new SoapVar(new KeyValuePair('key2', 'value2'), XSD_ANYTYPE, 'KeyValuePair');
$parameters = array(
    'prop1' => value1,
    'prop2' => value2,
    'prop3' => array($kvp1, $kvp2)
);
$request = array($parameters);
$client->__soapCall('SoapFunction', $request);

BTW: If the generated request xml has litter difference with one you desired, you could override the __doRequest of SoapClient to do some preg_replace etc.

You can generate your XML string like this and then pass it on to your soapFunction

<?php
$prop1="prop1 value";
$prop2="prop2 value";
$prop3Values=array();
$prop3Values["key1"]="value1";
$prop3Values["key2"]="value2";
$prop3Values["key3"]="value3";
$prop3Values["key3"]="value3";
$prop3=="";

foreach($prop3Values as $k=>$v)
{
if($prop3=="")
{
$prop3.="<KeyValuePair> 
        <Key>$k</Key> 
        <Value>$v</Value> 
    </KeyValuePair>";
}
else
{
    $prop3.="
    <KeyValuePair> 
        <Key>$k</Key> 
        <Value>$v</Value> 
    </KeyValuePair>";
}
}

$xml=<<<XML
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"> 
<SOAP-ENV:Body> 
<SoapFunction xmlns="http://services.***/"> 
    <prop1>$prop1</prop1> 
    <prop2>$prop2</prop2> 
    <prop3> 
    $prop3
    </prop3> 
</SoapFunction> 
</SOAP-ENV:Body> 
</sSOAP-ENV:Envelope>
XML;
echo $xml;
?>

In the variable $xml is your formatted string

I think you can just pass the complex structure.

Look here:

https://stackoverflow.com/questions/2608626/how-to-send-an-array-of-complex-type-in-php-using-soap-client

PHP can translate complex structures inside that call.