I need to send Json with one of the parameters being an array . The following code works perfectly .
$html = $client->post($url,
['json'=>[ 'requestData'=>
[
'sessionID'=>'261-7306141-0539957'
]
,
'productTargets'=>
[
[ 'ProductID'=>$data[0] ],
[ 'ProductID'=>$data[1] ],
[ 'ProductID'=>$data[2] ],
[ 'ProductID'=>$data[3] ],
[ 'ProductID'=>$data[4] ],
],
] ]
);
As you can see I am manually entering data[0],data[1],data[2] in productTargets . I need to send multiple like 100's of 'ProductID' object . I tried using
json_encode( [ [ 'ProductID'=>$data[0] ],
['ProductID'=>$data[2] ],
['ProductID'=>$data[3] ],
['ProductID'=>$data[4] ], ]
)
But it's not working . How do I send the data
Perhaps an approach like this:
<?php
$data =
[
'json' =>
[
'requestData'=>
[
'sessionID'=>'261-7306141-0539957'
]
]
];
$ids = range(1, 4); // Some dummy ids.
shuffle($ids);
$product_ids = array_map(function($v) {
return ['ProductID' => $v];
}, $ids);
$data['json']['productTargets'] = $product_ids;
var_dump(json_encode($data, JSON_PRETTY_PRINT));
Example output:
string(383) "{
"json": {
"requestData": {
"sessionID": "261-7306141-0539957"
},
"productTargets": [
{
"ProductID": 4
},
{
"ProductID": 3
},
{
"ProductID": 1
},
{
"ProductID": 2
}
]
}
}"