1

I have an array like this:

Array ( [10209064245580796] => Array ( [0] => Array ( [hashed_token] => ) [1] => Array ( [password] => ) [2] => Array ( [email] => klemen.pevc@gmail.com ) ) [10207252567926988] => Array ( [0] => Array ( [hashed_token] => ) [1] => Array ( [password] => 716b64c0f6bad9ac405aab3f00958dd1 ) [2] => Array ( [email] => milvuk@gmail.com ) ) ) 

That I made this way: $users looks like this:

Array ( [0] => 10209064245580796 [1] => 10207252567926988)

$arrayOfFields looks like this:

Array ( [0] => hashed_token [1] => password [2] => email )

So:

$array=array();
foreach($users as $user){
   $array[$user]=array();
   foreach($arrayOfFields as $getFieldValue) {
      $user = '' . $user . '';
      $query = "SELECT `$getFieldValue` FROM $table WHERE `$column`= $user";
      $result = $mysqli->query($query);
      $fetchResult = $result->fetch_assoc();
      $getFieldValue = '' . $getFieldValue . '';
      $finalValue = $fetchResult[$getFieldValue];
      array_push($array[$user] ,array($getFieldValue=>$finalValue));
   }
} 

And after this two foreachs I get an array $array as shown above in first example that I'm sending via cURL with this code:

$data = array('facebook_app_id' => $facebook_app_id, 'facebook_ids' => $facebook_ids,'values_for_custom_fields' => $array);

   $endpoint_url = 'https://servis-racunara.net/api/index.php';
   $curl = curl_init($endpoint_url);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Authorization: Token ".$token));
   curl_setopt($curl, CURLOPT_POST, 1);
   curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
   curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
   $curl_response = curl_exec($curl);
   curl_close($curl);

   //you do not need to print results, this is just for debugging purposes
   $result = $curl_response;
   print_r($result);

So inside script of this endpoint https://servis-racunara.net/api/index.php (on which I'm sending some data, including $array) when i do print_r($_POST) I get an array like this:

Array ( [facebook_app_id] => 1512823699024883 [facebook_ids] => 10209064245580796,10207252567926988[values_for_custom_fields] => Array )

So, under key values_for_custom_fields is stored $array array that I need to process. When I do print_r($_POST['values_for_custom_fields']); it just says 'Array', and when I try this:

foreach($_POST['values_for_custom_fields'] as $anything) {
       echo($anything);//same with print_r
}

Server says :

Warning: Invalid argument supplied for foreach()

Any ideas?

Ognj3n
  • 759
  • 7
  • 27
  • possible duplicate of [this](http://stackoverflow.com/questions/13665532/how-to-get-array-value-as-return-value-on-curl-php-request) look on [this](http://stackoverflow.com/questions/3772096/posting-multidimensional-array-with-php-and-curl) thread too – Tomasz Buczeń May 11 '16 at 07:07

1 Answers1

2

The problem is that you've chosen wrong format for sending data. In your code you use form submission format, namely application/x-www-form-urlencoded (as it is used by default in CURL). Neither of form submission formats support nested data (arrays). In other words you cannot send nested arrays in POST request as form-encoded data.

So you have to use other format to send your data. I would suggest to use JSON - popular format which allow any level of nesting. In this case you have to JSON-encode data before sending and decode - when receiving them in the endpoint script. Also you have to set appropriate Content-type header for request: application/json.

...
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "Authorization: Token ".$token,
    "Content-type: application/json"
));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
...
hindmost
  • 7,125
  • 3
  • 27
  • 39