Post multidimensional array using CURL and get the result on server Post multidimensional array using CURL and get the result on server arrays arrays

Post multidimensional array using CURL and get the result on server


cURL can only accept a simple key-value paired array where the values are strings, it can't take an array like yours which is an array of objects. However it does accept a ready made string of POST data, so you can build the string yourself and pass that instead:

$str = http_build_query($array);...curl_setopt($ch, CURLOPT_POSTFIELDS, $str);

A print_r($_POST) on the receiving end will show:

Array(    [0] => Array        (            [id] => 1        )    [1] => Array        (            [id] => 0        )    [2] => Array        (            [id] => 11        ))


I would give a go to serialize and unserialize:

1) Before sending your array, serialize it (and set your transfer mode to binary):

(...)curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);           // need this to post serialized datacurl_setopt($ch, CURLOPT_POSTFIELDS, serialize($array));  // $array is my above data

2) When you receive the data, unserialize it:

$array = unserialize($_POST);

More details here and here


$param['sub_array'] = json_encode($sub_array);

and on the other side

$sub_array= json_decode($_POST['sub_array']);