The problem of posting a multidimensional array using PHP cURL

I'm having trouble sending an array using PHP cURL. I have successfully published other values ​​on the same page using POST variables. But this is hard to understand. The only problem is how should I submit the data to the server.

I checked the original form using the form analyzer. And the form analyzer shows that the POST variables are sent as follows:

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

Values ​​are for illustrative purposes only. But they will be the same length.

My problem is that the responding server does not recognize the values ​​when I send them as follows:

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

My question is: how to send data so that the server understands this?

Thanks!

+4
source share
4 answers
 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)); 
+3
source

As Sefer Lajevardi says, you should use:

 curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($your_array)); 
+2
source

You need to set the message to true. Then you can pass an associative array in the POSTFIELDS options. as shown below.

 curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $your_array); 
0
source

Try the following:

  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);
 }
0
source

All Articles