简体   繁体   English

在PHP中使用cURL发送时无法打印多维数组

[英]Trouble printing multi-dimensional array sent with cURL in PHP

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: 我这样写的: $users看起来像这样:

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

$arrayOfFields looks like this: $arrayOfFields看起来像这样:

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: 在这两个foreachs我得到了一个数组$array ,如上面的第一个示例所示,我使用以下代码通过cURL发送:

$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: 因此,当我执行print_r($_POST)时,在此端点的脚本https://servis-racunara.net/api/index.php (我正在向其中发送一些数据,包括$array )的脚本中获得以下数组:

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. 因此,在键values_for_custom_fields下存储$array我需要处理的$array数组。 When I do print_r($_POST['values_for_custom_fields']); 当我做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() 警告:为foreach()提供了无效的参数

Any ideas? 有任何想法吗?

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). 在您的代码中,您使用表单提交格式 ,即application/x-www-form-urlencoded (因为在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. 换句话说,您不能将POST请求中的嵌套数组作为形式编码的数据发送。

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. 我建议使用JSON-允许任何级别的嵌套的流行格式。 In this case you have to JSON-encode data before sending and decode - when receiving them in the endpoint script. 在这种情况下,在端点脚本中接收数据时,必须在发送和解码之前对数据进行JSON编码。 Also you have to set appropriate Content-type header for request: application/json . 另外,您还必须为请求设置适当的Content-type标头: 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));
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM