51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
function callGeminiApi($prompt, $apiKey) {
|
|
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' . $apiKey;
|
|
|
|
$data = [
|
|
'contents' => [
|
|
[
|
|
'parts' => [
|
|
[
|
|
'text' => $prompt
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
|
|
$json_data = json_encode($data);
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: application/json',
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$curl_error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($curl_error) {
|
|
return ['error' => 'cURL Error: ' . $curl_error];
|
|
}
|
|
|
|
if ($http_code !== 200) {
|
|
return ['error' => 'API responded with HTTP code ' . $http_code, 'response' => json_decode($response, true)];
|
|
}
|
|
|
|
$result = json_decode($response, true);
|
|
|
|
if (isset($result['candidates'][0]['content']['parts'][0]['text'])) {
|
|
return ['success' => true, 'html' => $result['candidates'][0]['content']['parts'][0]['text']];
|
|
} else {
|
|
return ['error' => 'Could not extract content from API response.', 'response' => $result];
|
|
}
|
|
}
|