-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.php
45 lines (39 loc) · 1.42 KB
/
image.php
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
<?php
require ("config.php");
$request = $_POST['request'];
$image_url = $_POST['image'];
if (empty($request)) {echo("Empty request, please ask a real question!");exit();}
if (empty($image_url)) {echo("Empty image, please upload an image");exit();}
//setup the request, you can also use CURLOPT_URL
$curl = curl_init($apiurl);
// Returns the data/output as a string instead of raw data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
$input = str_replace(["_QUESTION_","_IMAGEURL_"], [$request, $image_url], $tem_img);
//Set the headers
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $_ENV['GITHUB_TOKEN']
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $input);
// get stringified data/output. See CURLOPT_RETURNTRANSFER
$data = curl_exec($curl);
// close curl resource to free up system resources
curl_close($curl);
if ($data) {
$dec = json_decode($data,true);
if (isset($dec['error'])) {
exit($dec['error']['message']);
}
if (is_array($dec['choices'])) {
$choices = $dec['choices'][0];
$Parsedown = new Parsedown();
echo $Parsedown->text($choices['message']['content']);
echo '<br><img style="width:60%" src="'.$image_url.'">';
} else {
echo "Could not parse API response. More: ";
print_r($dec);
}
} else {
echo "There was an error in the request";
}