PHP cURL is it possible to pass a multi-associative array?

Is it possible to send a multi-associative array to a page using cURL in php?

I am able to pass an array, but the following happens:

 // Open Connection
    $ch = curl_init();

    // Set the URL
    curl_setopt($ch, CURLOPT_URL, $this->config['submission']['eyerys']);

    // Set the number of fields being sent:
    curl_setopt($ch,CURLOPT_POST,count($this->call['info']));

    // The string to send:
    curl_setopt($ch,CURLOPT_POSTFIELDS,$string);

    // Return transfer:
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // SSL verification:
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    // Execute the post:
    $result = curl_exec($ch);

    $this->pre($result);

    // Close connection:
    $curl_close($ch);

I get the following output:

 Array
 (
        [info] => Array
        [answers] => Array
        [errors] => Array
 )

Answers


Nope, since curl cannot know how you want to encode it. Not every server-side language/framework uses the same way. I think PHP is the only language where the user can create an array by simply sending data with keys containing []. For example. in the python world one would just send the same value twice and then use a different function (such as .getlist('key') - depends on the framework though) to access the array instead of just a single value.

If you have control over the remote script, consider using something standardized such as JSON. Instead of sending a formencoded POST string either send a pure JSON body or a single formencoded POST value containing the JSON.

If you don't, you'll most likely have to encode the POST data on your own.


Need Your Help

Django form wizard - customized form layout for each step

django django-forms django-templates django-formwizard

At the moment I'm experimenting with django form wizard. The basic set-up works now and I'm able to call different templates for each step showing variable text.

Setting up Play 2.4.0 with Postgres and HikariCP yields configuration error

java postgresql playframework datasource hikaricp

I'm trying to set up a play framework server with a connection to a local postgres.