The data I want to post into the json api body below is from a while loop. So I want to Select from the database and send multiple students data from the database. I can post individually but I'm unable to do it from a loop. How please? Thanks.
{
"students":[
{
"admissionNumber": "2010",
"class":"js one"
},
{
"admissionNumber": "2020",
"class":"ss one"
}
],
"appDomain":"www.example.com"
}
Here's my code:
if(isset($_POST['submit'])){
$sql = "SELECT * FROM students";
while($row = mysqli_fetch_array($sql){
$admissionNumber = $row['admNo'];
$class = $row['class'];
$data = array('students' => array(['admissionNumber' => $admNo, 'class' => $class]),
'appDomain' => "http://example.com",
);
echo $data;
$url = 'https://the-end-point';
$params = $data;
$data_string = json_encode($data);
$curl = curl_init();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
echo "<script>
alert(' Student Created Successfully!');
window.location = '.../students.php';
</script>";
}
}
}