I'm trying to call a REST endpoint that is expecting an object with an array 'campaigns' and a boolean 'getChildren'. The issue is that the array is sometimes serialized as an object instead of an array.
The incorrect request I'm getting :
{
"campaigns":
{
"1":"1006352",
"2":"1006347",
"3":"1006350",
"4":"1006349",
"5":"1006348",
"6":"1006345",
"7":"1006344",
"8":"1006343"
},
"getChildren":false
}
What I want (and that I get sometimes) :
{
"campaigns":["1006351","1006346"],
"getChildren":false
}
Here is my code :
$campaignIds = array_map(function ($item) use ($networkIds) {
return $item->nid;
},
array_filter($items, function ($item) use ($networkIds) {
return empty((int)$item->nb_shared);
}));
$consoWithoutChildren = EndpointClient::getConsumptionByCampaign($networkIds, $campaignIds);
I want the 'campaigns' parameter to be always interpreted as an array.
I tried json_encode() but it escapes the array, causing issues in the use cases where I had valid JSON, like that :
{"campaigns":"[\"1006351\",\"1006346\"]","getChildren":false}
Any idea what is wrong ?
json_encode()escapes an array in any way. Also, nobody here knows what happens in the other parts of your program or what e.g.$networkIdsis.