Search code examples
phparraysxmlsoapsoap-client

PHP SoapClient Request - Array Structure


I've been successfully using PHP and SoapClient to pass requests to a third party API using array. By dumping out the request from SoapClient what the XML data that is being passed to the API.

Now I've come across a required XML structure for <extendedData> that I'm having trouble passing as an array;

<TypeID>int</TypeID>
<FooID>int</FooID>
<BarID>int</BarID>
<extendedData>
    <Service_CreateFields>
        <FieldName>my string 1</FieldName>
        <FieldValue>my string 2</FieldValue>
        <Comments>my string 3</Comments>
    </Service_CreateFields>
    <Service_CreateFields>
        <FieldName>my string 4</FieldName>
        <FieldValue>my string 5</FieldValue>
        <Comments>my string 6</Comments>
    </Service_CreateFields>
</extendedData>

I've tried the following array but it fails to generate an XML request with two or more <Service_CreateFields>

$data = array(
    "TypeID" => "11",
    "FooID" => "22",
    "BarID" => "33",
    "extendedData" => array(
        "Service_CreateFields" => array(
                "FieldName" => "my string 1",
                "FieldValue" => "my string 2",
                "Comments" => "my string 3",
            ),
            "Service_CreateFields" => array(
                "FieldName" => "my string 4",
                "FieldValue" => "my string 5",
                "Comments" => "my string 6",
            ),
    ),
);

Has anyone had experience with this?


Solution

  • Try wrapping each child in it's own array:

    $data = array(
        "TypeID" => "11",
        "FooID" => "22",
        "BarID" => "33",
        "extendedData" => array(
            "Service_CreateFields" => array(
                array(
                    "FieldName" => "my string 1",
                    "FieldValue" => "my string 2",
                    "Comments" => "my string 3",
                ),
                array(
                    "FieldName" => "my string 4",
                    "FieldValue" => "my string 5",
                    "Comments" => "my string 6",
                )
            )
        )
    );