-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion2.php
More file actions
72 lines (53 loc) · 1.79 KB
/
question2.php
File metadata and controls
72 lines (53 loc) · 1.79 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
<?php
function MyQuestion($question) {
$apiKey = "1929283787xnsj29383882fx"; // let asume this is my API key
$url = "https://googlegemin.com/api/gemini?key=" . $apiKey;
$data = [
"contents" => [
[
"parts" => [
["text" => $question]
]
]
]
];
$header = [
"http" => [
"header" => "Content-Type: application/json",
"method" => "POST",
"content" => json_encode($data),
],
];
$context = stream_context_create($header);
// use file_get_contents to get the content from url and store it
$result = file_get_contents($url, false, $context);
// check error
if ($result === FALSE) {
return "Error fetching response from Gemini API.";
}
// use json_decode funtion to convert to object or array if you put true in the second params it means to convert to array
$response = json_decode($result, true);
// Now let Extract the answer from the response to avoid error let check if it is set first
if (isset($response["candidates"][0]["content"]["parts"][0]["text"])) {
return $response["candidates"][0]["content"]["parts"][0]["text"];
} else {
return "No valid response from Gemini API.";
}
}
?>
<!-- // we can use form to as an input to ask this qestion like -->
<!-- // Ask the question
$question = "Who is Donald Trump?"; -->
<form action="" method="post">
<label for="question">
<input type="text" name="qest" id="">
</label>
</form>
<?php
if(count($_POST) >0){
$question = $_POST['quest'];
//Now run the function
$answer = MyQuestion($question);
//echo the answer
echo $answer;
}