I am trying to send data, via POST, to a second PHP script. The script processes the data, then returns a stream of JSON text. When I send data via file_get_contents(), I get the first level array of details passed ok, but the second level of array data does not pass.
More info: I have created a small form that must update itself via POST to an external API file. The problem exists in the html checkboxes, as a checkbox is passed as an array (userGroup).
To make this process work, I must POST the results back to the form page, then read the POST'ed results and send it via POST through file_get_contents.
Sample code is below. userGroup is the multidimensional array:
if($_POST['update']=="Update")
{
$API_URL=urldecode($API_URL);
$post_data_keyed_array=array();
foreach($_POST as $key=>$value)
{
$$key=$value;
if($key!='update' || $key!='userGroup') $post_data_keyed_array[$key]=$value;
}
foreach($userGroup as $key=>$value)
{
$push='userGroup['.$key.']='.$value;
array_push($post_data_keyed_array,$push);
}
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($post_data_keyed_array),
'ignore_errors' => true
)
);
//stream encode the data
$context = stream_context_create($opts);
//run the data via GET or POST through the data and get the results
$result = file_get_contents("$API_URL", false, $context);
echo $result;
}
Can someone please tell me what I'm doing wrong or give an example of how it might be better implemented? I'm not sure if I'm allowed to use cURL or not, which is why I'm using file_get_contents.