Post multidimensional array using CURL and get the result on server

14,886

Solution 1

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
        )

)

Solution 2

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

and on the other side

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

Solution 3

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 data
curl_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

Share:
14,886
Yogesh Suthar
Author by

Yogesh Suthar

SOreadytohelp Working in Nykaa as Tech Lead(iOS). I love to work in Swift for iOS. For cookies :- Disable Trace , httpOnly , Use SSL Cookies , Signed origin Contact email-id : yogesh[dot]mistry89[at]gmail[dot]com

Updated on June 09, 2022

Comments

  • Yogesh Suthar
    Yogesh Suthar almost 2 years

    I am sending data from my local machine to server using CURL. And the data is multidimensional array.

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

    I am using this below code for sending the data.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, "my_url");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $array);  // $array is my above data
    

    But at server when I try to put this incoming data to file or just print_r it gives me this below output

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

    But I want the output in multidimensional.

    I tried with print_r($_POST[0]) but it gives only Array text.

  • Yogesh Suthar
    Yogesh Suthar over 11 years
    Thanks for answering, let you know when I will integrate this in my code. I have one more question ,I tried with sending array of array but at receiving end it gives the same output Array ( [0] => Array [1] => Array [2] => Array )
  • MrCode
    MrCode over 11 years
    Yes, cURL can't take an array of arrays or an array of objects, it can only take a one dimensional array of strings, unless you build the post data manually (as in the answer).
  • Yogesh Suthar
    Yogesh Suthar over 11 years
    Thanks MrCode today I have implemented your code and now it is working as I wanted. Thanks again.
  • Valter Lorran
    Valter Lorran about 10 years
    great, but it didnt work this way, i had to curl_setopt($ch, CURLOPT_POSTFIELDS, array('data'=>serialize($array))); and then $array = unserialize($_POST['data']);
  • Yogesh Suthar
    Yogesh Suthar almost 8 years
    Well I don't remember that whether I have tried this way. But may be useful for other users. :)