Search code examples
phpapisoapsoap-client

PHP SOAP: Passing the same parameters with the same name


I can't get this to work. I've read a bunch on it on the forum already but I just can't seem to find the solution to this.

I've created a SOAP call and it's working etc., but when I try to pass the same parameters multiple times it just overwrites itself which is logical.

The code has to be done with objects only so I've used stdClass()

Example of the code below:

$relationCreate = new stdClass();

$relationCreate->credentials = new stdClass();
$relationCreate->credentials->ApiKey = ''; //Removed for security reasons.
$relationCreate->credentials->DatabaseId = ''; //Removed for security reasons.;
$relationCreate->credentials->UserId = ''; //Removed for security reasons.;

$relationCreate->parentRelationId = $company;
$relationCreate->relationEntityTypeId = "84a15869-5b88-49df-ad47-7b6f9648ae07";

//surname
$relationCreate->relationFieldValues = new stdClass();
$relationCreate->relationFieldValues->PvFieldValueData = new stdClass();
$relationCreate->relationFieldValues->PvFieldValueData->Id = "9d549512-dc8a-4774-84d1-27a349e8a8c7";
$relationCreate->relationFieldValues->PvFieldValueData->Value = $name;

// This one has to repeat which does not work. Which is logical
$relationCreate->relationFieldValues = new stdClass();
$relationCreate->relationFieldValues->PvFieldValueData = new stdClass();
$relationCreate->relationFieldValues->PvFieldValueData->Id = "9d549512-dc8a-4774-84d1-27a349e8a8c7";
$relationCreate->relationFieldValues->PvFieldValueData->Value = $name;

The soap should look as followed I tested this using SoapUI:

<api:fieldValues>
    <!--Zero or more repetitions:-->
    <api:PvFieldValueData>
        <api:Id>c2fcb464-92e6-4227-8672-56f88e219279</api:Id>
        <!--Optional:-->
        <api:Value>Test</api:Value>
    </api:PvFieldValueData>
</api:fieldValues>

<api:fieldValues>
<!--Zero or more repetitions:-->
<api:PvFieldValueData>
    <api:Id>d900fe23-8549-451c-82f4-c5918cb3abbb</api:Id>
    <!--Optional:-->
    <api:Value>Test</api:Value>
</api:PvFieldValueData>
</api:fieldValues>
     
 

WSDL file for reference: https://api.perfectview.nl/V1/perfectview.asmx?WSDL

References: PHP SoapClient - Multiple attributes with the same key

SoapClient: how to pass multiple elements with same name?


Solution

  • You can try to put the items into an array.

    Example:

    $relationCreate->relationFieldValues = [];
    
    // repeat this in a foreach loop:
    $item = new stdClass();
    $item->PvFieldValueData = new stdClass();
    $item->PvFieldValueData->Id = $uuid;
    $item->PvFieldValueData->Value = $name;
    
    // Add item to values
    $relationCreate->relationFieldValues[] = $item;