This repository was archived by the owner on Dec 12, 2023. It is now read-only.
generated from course-files/BBT4206-Lab15of15-ConsumePlumberAPIOutput
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab15-ConsumePlumberAPIOutput.-Submission.php
More file actions
140 lines (114 loc) · 4.49 KB
/
Lab15-ConsumePlumberAPIOutput.-Submission.php
File metadata and controls
140 lines (114 loc) · 4.49 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html>
<head>
<title>Symptoms Prediction Form</title>
</head>
<body>
<h2>Symptoms Prediction</h2>
<form method="post" action="prediction.php">
<!-- Input fields for symptoms -->
<label for="Symptom_1">Symptom 1:</label>
<input type="text" id="Symptom_1" name="Symptom_1"><br><br>
<label for="Symptom_2">Symptom 2:</label>
<input type="text" id="Symptom_2" name="Symptom_2"><br><br>
<label for="Symptom_3">Symptom 3:</label>
<input type="text" id="Symptom_3" name="Symptom_3"><br><br>
<label for="Symptom_4">Symptom 4:</label>
<input type="text" id="Symptom_4" name="Symptom_4"><br><br>
<label for="Symptom_5">Symptom 5:</label>
<input type="text" id="Symptom_5" name="Symptom_5"><br><br>
<label for="Symptom_6">Symptom 6:</label>
<input type="text" id="Symptom_6" name="Symptom_6"><br><br>
<label for="Symptom_7">Symptom 7:</label>
<input type="text" id="Symptom_7" name="Symptom_7"><br><br>
<label for="Symptom_8">Symptom 8:</label>
<input type="text" id="Symptom_8" name="Symptom_8"><br><br>
<!-- Add more input fields for other symptoms (Symptom_3, Symptom_4, etc.) -->
<input type="submit" value="Predict">
</form>
</body>
</html>
<?php
# *****************************************************************************
# Lab 14: Consume data from the Plumber API Output (using PHP) ----
#
# Course Code: BBT4206
# Course Name: Business Intelligence II
# Semester Duration: 21st August 2023 to 28th November 2023
#
# Lecturer: Allan Omondi
# Contact: aomondi [at] strathmore.edu
#
# Note: The lecture contains both theory and practice. This file forms part of
# the practice. It has required lab work submissions that are graded for
# coursework marks.
#
# License: GNU GPL-3.0-or-later
# See LICENSE file for licensing information.
# *****************************************************************************
// Full documentation of the client URL (cURL) library: https://www.php.net/manual/en/book.curl.php
// STEP 1: Set the API endpoint URL
$apiUrl = 'http://127.0.0.1:5022/symptoms';
// Initiate a new cURL session/resource
$curl = curl_init();
// STEP 2: Set the values of the parameters to pass to the model ----
// Modify the symptoms values accordingly
$Symptom_1 = 'symptom1_value';
$Symptom_2 = 'symptom2_value';
$Symptom_3 = 'symptom3_value';
$Symptom_4 = 'symptom4_value';
$Symptom_5 = 'symptom5_value';
$Symptom_6 = 'symptom6_value';
$Symptom_7 = 'symptom7_value';
$Symptom_8 = 'symptom8_value';
/*
$Symptom_1 = 'example_value_1';
$Symptom_2 = 'example_value_2';
$Symptom_3 = 'example_value_3';
$Symptom_4 = 'example_value_4';
$Symptom_5 = 'example_value_5';
$Symptom_6 = 'example_value_6';
$Symptom_7 = 'example_value_7';
$Symptom_8 = 'example_value_8';*/
$params = array('Symptom_1' => $Symptom_1, 'Symptom_2' => $Symptom_2,
'Symptom_3' => $Symptom_3, 'Symptom_4' => $Symptom_4,
'Symptom_5' => $Symptom_5, 'Symptom_6' => $Symptom_6,
'Symptom_7' => $Symptom_7, 'Symptom_8' => $Symptom_8);
// STEP 3: Set the cURL options
// CURLOPT_RETURNTRANSFER: true to return the transfer as a string of the
// return value of curl_exec() instead of outputting it directly.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$apiUrl = $apiUrl . '?' . http_build_query($params);
curl_setopt($curl, CURLOPT_URL, $apiUrl);
// For testing:
echo "The generated URL sent to the API is:<br>".$apiUrl."<br><br>";
// Make a GET request
$response = curl_exec($curl);
// Check for cURL errors
if (curl_errno($curl)) {
$error = curl_error($curl);
// Handle the error appropriately
die("cURL Error: $error");
}
// Close cURL session/resource
curl_close($curl);
// Process the response
// echo "<br>The predicted output in JSON format is:<br>" . var_dump($response) . "<br><br>";
// Decode the JSON into normal text
$data = json_decode($response, true);
// echo "<br>The predicted output in decoded JSON format is:<br>" . var_dump($data) . "<br><br>";
// Check if the response was successful
if (isset($data['0'])) {
// API request was successful
// Access the data returned by the API
echo "The predicted status is:<br>";
// Process the data
foreach($data as $repository) {
echo $repository['0'], $repository['1'], $repository['2'], "<br>";
}
} else {
// API request failed or returned an error
// Handle the error appropriately
echo "API Error: " . $data['message'];
}
?>