4

I'm having problems posting an array using PHP cURL. I have successfully posted other values to the same page using POST-variables. But this one is hard to figure out. The only problem is how I should present the data to the server.

I checked the original form using a form analyzer. And the form analyzer shows that the POST variables are sent like this:

array fundDistribution' => 
     array
        204891 => '20' (length=2)
        354290 => '20' (length=2)
        776401 => '20' (length=2)
        834788 => '40' (length=2)

The values are just for showing an example. But they will be the same length.

My problem is that the responding server does not recognise the values when I send them like this:

Array(
[104786] => 20
[354290] => 20
[865063] => 20
[204891] => 20
[834788] => 20)

My question is: How do I send the data so the server understands it?

Thank you!

Christopher
  • 41
  • 1
  • 3
  • You are not showing where the data comes from and how it takes the weird shape you show in your second example, so it's impossible to make any suggestions on how to change it – Pekka Oct 06 '10 at 16:29
  • Sorry, I have edited the original post. – Christopher Oct 06 '10 at 16:40
  • still need more code! Show us where this array is defined, and the cURL PHP functions where you send the data. – chigley Oct 06 '10 at 16:46
  • [Similar post](http://stackoverflow.com/a/8224117/1057527) worked for me. I haven't tried Wrikken answer here. – machineaddict Nov 07 '14 at 09:37

4 Answers4

3

As the Sepehr Lajevardi says, You should use:

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($your_array));
TomaszKane
  • 805
  • 1
  • 11
  • 26
3
function flatten_GP_array(array $var,$prefix = false){
        $return = array();
        foreach($var as $idx => $value){
                if(is_scalar($value)){
                        if($prefix){
                                $return[$prefix.'['.$idx.']'] = $value;
                        } else {
                                $return[$idx] = $value;
                        }
                } else {
                        $return = array_merge($return,flatten_GP_array($value,$prefix ? $prefix.'['.$idx.']' : $idx));
                }
        }
        return $return;
}
//...
curl_setopt($ch, CURLOPT_POSTFIELDS,flatten_GP_array($array));
Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • Thank you, what should I use as prefix? A "&" or "fundDistribution" ? – Christopher Oct 06 '10 at 16:52
  • Nope, at the start, don't give a prefix, just leave the second parameter out of it. it's only meant to be set on recursion. I do however expect you to send an `array('fundDistribution'=>array(...rest..));` as input. – Wrikken Oct 06 '10 at 16:53
  • Yes, and I get something like this: Array ( [fundDistribution[104786]] => 20 ... ) – Christopher Oct 06 '10 at 16:55
  • Hmm, that wording might be off. _"I do expect you to"_ => _"I expect that you"_ ;) – Wrikken Oct 06 '10 at 16:56
  • @Christopher: yup, and that is something cURL understands (it does not get multidimensional array, only flat ones, which this function makes). – Wrikken Oct 06 '10 at 16:57
  • Okay, thank you for your help. The problem is that the server is still not getting the data. I will continue to search for other problems, because the site in question uses JavaScript alot. – Christopher Oct 06 '10 at 16:58
  • I found the problem, they use a JavaScript cookie to pass the parameters... REALLY stupid, but I should be able to find a work-a-round. – Christopher Oct 06 '10 at 17:04
0

You need to set post to true. Then you can pass the associative array in the POSTFIELDS option. as shown below.

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $your_array);
Mitch C
  • 553
  • 3
  • 8
  • Thank you for your answer! I have set a lot of options. Among those, the options that you refer to are set as well. – Christopher Oct 06 '10 at 16:47
  • Passing an array to CURLOPT_POSTFIELDS changes the behavior of cURL a little. It will send the data as multiform/post-data and any parameters with a value that begins with '@' will be treated as filenames. – bobdiaes Oct 06 '10 at 17:08
0

Try this:

function postVars($vars,$sep='&') {
    $str = '';
    foreach( $vars as $k => $v) {
        if(is_array($v)) {
            foreach($v as $vk=>$vi) {
                $str .= urlencode($k).'['.$vk.']'.'='.urlencode($vi).$sep;
            }
        } else {
            $str .= urlencode($k).'='.urlencode($v).$sep;
        }
    }
    return substr($str, 0, -1);
}
bobdiaes
  • 1,120
  • 11
  • 9