1

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
 )
4

1 回答 1

1

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.

于 2012-06-11T10:51:02.193 回答