Repository navigation
Expand file tree
/
Copy pathcalculadora_api.php
More file actions
66 lines (56 loc) · 2.17 KB
/
Copy pathcalculadora_api.php
File metadata and controls
66 lines (56 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
header("Content-Type: application/json");
// Check if request is POST
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
echo json_encode(["error" => "Method not allowed"]);
exit;
}
// Get parameters from POST request
$gpa = isset($_POST['gpa']) ? floatval($_POST['gpa']) : null;
$sex = isset($_POST['sex']) ? $_POST['sex'] : null;
$university = isset($_POST['university']) ? $_POST['university'] : null;
$course = isset($_POST['course']) ? $_POST['course'] : null;
$country = isset($_POST['country']) ? $_POST['country'] : null;
$profit_threshold = isset($_POST['profit_threshold']) ? floatval($_POST['profit_threshold']) : null;
// Validate required parameters
if ($gpa === null || $sex === null || $university === null || $course === null || $country === null || $profit_threshold === null) {
echo json_encode(["error" => "Missing required parameters"]);
exit;
}
// Map sex values to match Python script requirements (0 for male, 1 for female)
$gender = ($sex === 'female') ? 1 : 0;
// Execute Python script with parameters
$pythonScript = '../edubridge_algoritmo/calculate_loanOptions.py';
$command = "python3 " . escapeshellarg($pythonScript) . " " .
escapeshellarg($gpa) . " " .
escapeshellarg($gender) . " " .
escapeshellarg($university) . " " .
escapeshellarg($course) . " " .
escapeshellarg($country) . " " .
escapeshellarg($profit_threshold);
$output = [];
$return_val = 0;
exec($command, $output, $return_val);
// Check if Python script executed successfully
if ($return_val !== 0) {
echo json_encode(["error" => "Failed to execute calculation script"]);
exit;
}
// Process Python script output
$results = [];
foreach ($output as $line) {
// Expected format: "interest_rate,min_loan,max_loan"
$parts = explode(',', $line);
if (count($parts) === 3) {
$taxa_juros = floatval($parts[0]);
$min_loan = floatval($parts[1]);
$max_loan = floatval($parts[2]);
$results[] = [
"taxa_juros" => $taxa_juros,
"intervalos_emprestimos" => [$min_loan, $max_loan]
];
}
}
// Return results
echo json_encode(["results" => $results]);
?>