Reputation: 21
<!-- language: lang-html -->
<?php
function generateAIContent($apiKey, $prompt) {
$url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={$apiKey}";
$postData = json_encode([
"contents" => [
[
"parts" => [
["text" => $prompt]
]
]
],
"generationConfig" => [
"temperature" => 0.9,
"topK" => 1,
"topP" => 1,
"maxOutputTokens" => 2000,
"stopSequences" => []
],
"safetySettings" => [
[
"category" => "HARM_CATEGORY_HARASSMENT",
"threshold" => "BLOCK_ONLY_HIGH"
],
[
"category" => "HARM_CATEGORY_HATE_SPEECH",
"threshold" => "BLOCK_ONLY_HIGH"
],
[
"category" => "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold" => "BLOCK_ONLY_HIGH"
],
[
"category" => "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold" => "BLOCK_ONLY_HIGH"
]
]
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($postData)
]);
$response = curl_exec($ch);
curl_close($ch);
$responseData = json_decode($response, true);
if (isset($responseData['candidates'][0]['content']['parts'][0]['text'])) {
return $responseData['candidates'][0]['content']['parts'][0]['text'];
} else {
return "No generated text found.";
}
}
$apiKey="--------------";
$prompt="--------------";
$generatedText = generateAIContent($apiKey, $prompt);
echo $generatedText;
?>
<!-- end snippet -->
When tested on local machine, this code connect successfully to gemini API and return the desired results (i.e., returns the answer to the question in the prompt) but when moved to a live server (it will not give return an answer to the question in the prompt.it return "No generated text found."). I can't figure out why, do you guys have any idea? Is there a problem in api key or problem in server. I try this on many different servers but no response from the gemini API is returned. but it will return an answer perfectly in my local machine.
I run the code on my local host, and it was able to connect to Gemini API and return an answer to the question in the prompt. I moved my code to a live server (free PHP hosting service) but the code there was not able to return an answer to the question in the prompt.
When it runs, $responseData
is NULL, and if I call print curl_error($ch)
I get "Failed to connect to generativelanguage.googleapis.com port 443: Connection refused"
Upvotes: 2
Views: 1326
Reputation: 50711
There are several possible causes of the problem, and the best way to diagnose them (as I mention in the comment above) is to update your question with the full contents of $responseData
.
However, the most likely cause is that your server is not in one of the available regions, while your home machine is.
Based on the error in your update (in the comments and question), the "connection refused" error seems to suggest that something is blocking your hosting machine or provider from accessing the Google server. If you are in control of the firewall, make sure outgoing connections work correctly.
Upvotes: 2