1. Go to this page and download the library: Download google-gemini-php/client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
google-gemini-php / client example snippets
use Gemini\Enums\ModelVariation;
use Gemini\GeminiHelper;
use Gemini;
$yourApiKey = getenv('YOUR_API_KEY');
$client = Gemini::client($yourApiKey);
$result = $client->generativeModel(model: 'gemini-2.0-flash')->generateContent('Hello');
$result->text(); // Hello! How can I assist you today?
// Helper method usage
$result = $client->generativeModel(
model: GeminiHelper::generateGeminiModel(
variation: ModelVariation::FLASH,
generation: 2.5,
version: "preview-04-17"
), // models/gemini-2.5-flash-preview-04-17
);
$result->text(); // Hello! How can I assist you today?
use Gemini;
$yourApiKey = getenv('YOUR_API_KEY');
$client = Gemini::factory()
->withApiKey($yourApiKey)
->withBaseUrl('https://generativelanguage.example.com/v1beta') // default: https://generativelanguage.googleapis.com/v1beta/
->withHttpHeader('X-My-Header', 'foo')
->withQueryParam('my-param', 'bar')
->withHttpClient($guzzleClient = new \GuzzleHttp\Client(['timeout' => 30])) // default: HTTP client found using PSR-18 HTTP Client Discovery
->withStreamHandler(fn(RequestInterface $request): ResponseInterface => $guzzleClient->send($request, [
'stream' => true // Allows to provide a custom stream handler for the http client.
]))
->make();
use Gemini;
$yourApiKey = getenv('YOUR_API_KEY');
$client = Gemini::client($yourApiKey);
$result = $client->generativeModel(model: 'gemini-2.0-flash')->generateContent('Hello');
$result->text(); // Hello! How can I assist you today?
use Gemini\Data\Blob;
use Gemini\Enums\MimeType;
$result = $client
->generativeModel(model: 'gemini-2.0-flash')
->generateContent([
'What is this picture?',
new Blob(
mimeType: MimeType::IMAGE_JPEG,
data: base64_encode(
file_get_contents('https://storage.googleapis.com/generativeai-downloads/images/scones.jpg')
)
)
]);
$result->text(); // The picture shows a table with a white tablecloth. On the table are two cups of coffee, a bowl of blueberries, a silver spoon, and some flowers. There are also some blueberry scones on the table.
use Gemini\Data\UploadedFile;
use Gemini\Enums\MimeType;
$result = $client
->generativeModel(model: 'gemini-2.0-flash')
->generateContent([
'What is this video?',
new UploadedFile(
fileUri: '123-456', // accepts just the name or the full URI
mimeType: MimeType::VIDEO_MP4
)
]);
$result->text(); // The video shows...
use Gemini\Data\ImageConfig;
use Gemini\Data\GenerationConfig;
$imageConfig = new ImageConfig(aspectRatio: '16:9');
$generationConfig = new GenerationConfig(imageConfig: $imageConfig);
$response = $client->generativeModel(model: 'gemini-2.5-flash-image')
->withGenerationConfig($generationConfig)
->generateContent('Draw a futuristic city');
// Save the image
file_put_contents('image.png', base64_decode($response->parts()[0]->inlineData->data));
use Gemini\Data\Content;
use Gemini\Enums\Role;
$chat = $client
->generativeModel(model: 'gemini-2.0-flash')
->startChat(history: [
Content::parse(part: 'The stories you write about what I have to say should be one line. Is that clear?'),
Content::parse(part: 'Yes, I understand. The stories I write about your input should be one line long.', role: Role::MODEL)
]);
$response = $chat->sendMessage('Create a story set in a quiet village in 1600s France');
echo $response->text(); // Amidst rolling hills and winding cobblestone streets, the tranquil village of Beausoleil whispered tales of love, intrigue, and the magic of everyday life in 17th century France.
$response = $chat->sendMessage('Rewrite the same story in 1600s England');
echo $response->text(); // In the heart of England's lush countryside, amidst emerald fields and thatched-roof cottages, the village of Willowbrook unfolded a tapestry of love, mystery, and the enchantment of ordinary days in the 17th century.
$stream = $client
->generativeModel(model: 'gemini-2.0-flash')
->streamGenerateContent('Write long a story about a magic backpack.');
foreach ($stream as $response) {
echo $response->text();
}
use Gemini\Data\GenerationConfig;
use Gemini\Data\Schema;
use Gemini\Enums\DataType;
use Gemini\Enums\ResponseMimeType;
$result = $client
->generativeModel(model: 'gemini-2.0-flash')
->withGenerationConfig(
generationConfig: new GenerationConfig(
responseMimeType: ResponseMimeType::APPLICATION_JSON,
responseSchema: new Schema(
type: DataType::ARRAY,
items: new Schema(
type: DataType::OBJECT,
properties: [
'recipe_name' => new Schema(type: DataType::STRING),
'cooking_time_in_minutes' => new Schema(type: DataType::INTEGER)
],
use Gemini\Data\Content;
use Gemini\Data\FunctionCall;
use Gemini\Data\FunctionDeclaration;
use Gemini\Data\FunctionResponse;
use Gemini\Data\Part;
use Gemini\Data\Schema;
use Gemini\Data\Tool;
use Gemini\Enums\DataType;
use Gemini\Enums\Role;
function handleFunctionCall(FunctionCall $functionCall): Content
{
if ($functionCall->name === 'addition') {
return new Content(
parts: [
new Part(
functionResponse: new FunctionResponse(
name: 'addition',
response: ['answer' => $functionCall->args['number1'] + $functionCall->args['number2']],
),
thoughtSignature: 'some-signature' // Optional: Required for some models (e.g. Gemini 3 Pro)
)
],
role: Role::USER
);
}
//Handle other function calls
}
$chat = $client
->generativeModel(model: 'gemini-2.0-flash')
->withTool(new Tool(
functionDeclarations: [
new FunctionDeclaration(
name: 'addition',
description: 'Performs addition',
parameters: new Schema(
type: DataType::OBJECT,
properties: [
'number1' => new Schema(
type: DataType::NUMBER,
description: 'First number'
),
'number2' => new Schema(
type: DataType::NUMBER,
description: 'Second number'
),
],
use Gemini\Data\CodeExecution;
use Gemini\Data\Tool;
$response = $client
->generativeModel(model: 'gemini-2.0-flash')
->withTool(new Tool(codeExecution: CodeExecution::from()))
->generateContent('What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.');
// Access the executed code and results
foreach ($response->parts() as $part) {
if ($part->executableCode !== null) {
echo "Language: " . $part->executableCode->language->value . "\n";
echo "Code: " . $part->executableCode->code . "\n";
}
if ($part->codeExecutionResult !== null) {
echo "Outcome: " . $part->codeExecutionResult->outcome->value . "\n";
echo "Output: " . $part->codeExecutionResult->output . "\n";
}
}
use Gemini\Data\GoogleSearch;
use Gemini\Data\Tool;
$response = $client
->generativeModel(model: 'gemini-2.0-flash')
->withTool(new Tool(googleSearch: GoogleSearch::from()))
->generateContent('Who won the Euro 2024?');
echo $response->text();
// Spain won Euro 2024, defeating England 2-1 in the final.
// Access grounding metadata to see sources
$groundingMetadata = $response->candidates[0]->groundingMetadata;
if ($groundingMetadata !== null) {
// Get the search queries that were executed
foreach ($groundingMetadata->webSearchQueries ?? [] as $query) {
echo "Search query: {$query}\n";
}
// Get the web sources
foreach ($groundingMetadata->groundingChunks ?? [] as $chunk) {
if ($chunk->web !== null) {
echo "Source: {$chunk->web->title} - {$chunk->web->uri}\n";
}
}
// Get grounding supports (links text segments to sources)
foreach ($groundingMetadata->groundingSupports ?? [] as $support) {
if ($support->segment !== null) {
echo "Text segment: {$support->segment->text}\n";
echo "Supported by chunks: " . implode(', ', $support->groundingChunkIndices ?? []) . "\n";
}
}
}
use Gemini\Data\GoogleMaps;
use Gemini\Data\RetrievalConfig;
use Gemini\Data\Tool;
use Gemini\Data\ToolConfig;
$tool = new Tool(
googleMaps: new GoogleMaps(enableWidget: true)
);
$toolConfig = new ToolConfig(
retrievalConfig: new RetrievalConfig(
latitude: 40.758896,
longitude: -73.985130
)
);
$response = $client
->generativeModel(model: 'gemini-2.0-flash')
->withTool($tool)
->withToolConfig($toolConfig)
->generateContent('Find coffee shops near me');
echo $response->text();
// (Model output referencing coffee shops)
use Gemini\Data\FileSearch;
use Gemini\Data\Tool;
$tool = new Tool(
fileSearch: new FileSearch(
fileSearchStoreNames: ['files/my-document-store'],
metadataFilter: 'author = "Robert Graves"'
)
);
$response = $client
->generativeModel(model: 'gemini-2.0-flash')
->withTool($tool)
->generateContent('Summarize the document about Greek myths by Robert Graves');
echo $response->text();
// (Model output summarizing the document)
use Gemini\Data\Content;
$response = $client
->generativeModel(model: 'gemini-2.0-flash')
->withSystemInstruction(
Content::parse('You are a helpful assistant that always responds in the style of a pirate. Use nautical terms and pirate slang in all your responses.')
)
->generateContent('Tell me about PHP programming');
echo $response->text();
// Ahoy there, matey! Let me tell ye about this fine treasure called PHP programming...
use Gemini\Data\Content;
use Gemini\Data\GenerationConfig;
use Gemini\Enums\ResponseMimeType;
$response = $client
->generativeModel(model: 'gemini-2.0-flash')
->withSystemInstruction(
Content::parse('You are a JSON API. Always respond with valid JSON objects. Be concise.')
)
->withGenerationConfig(
new GenerationConfig(responseMimeType: ResponseMimeType::APPLICATION_JSON)
)
->generateContent('Give me information about the Eiffel Tower');
print_r($response->json());
use Gemini\Data\GenerationConfig;
use Gemini\Data\SpeechConfig;
use Gemini\Data\VoiceConfig;
use Gemini\Data\PrebuiltVoiceConfig;
use Gemini\Enums\ResponseModality;
$response = $client->generativeModel('gemini-2.5-flash-preview-tts')->withGenerationConfig(
generationConfig: new GenerationConfig(
responseModalities: [ResponseModality::AUDIO],
speechConfig: new SpeechConfig(
voiceConfig: new VoiceConfig(
new PrebuiltVoiceConfig(voiceName: 'Kore')
),
)
)
)->generateContent("Say: Hello world");
// The response contains base64 encoded audio
$audioData = $response->parts()[0]->inlineData->data;
use Gemini\Data\GenerationConfig;
use Gemini\Data\SpeechConfig;
use Gemini\Data\MultiSpeakerVoiceConfig;
use Gemini\Data\PrebuiltVoiceConfig;
use Gemini\Data\SpeakerVoiceConfig;
use Gemini\Data\VoiceConfig;
use Gemini\Enums\ResponseModality;
$response = $client->generativeModel('gemini-2.5-flash-preview-tts')->withGenerationConfig(
generationConfig: new GenerationConfig(
responseModalities: [ResponseModality::AUDIO],
speechConfig: new SpeechConfig(
multiSpeakerVoiceConfig: new MultiSpeakerVoiceConfig([
new SpeakerVoiceConfig(
speaker: 'Joe',
voiceConfig: new VoiceConfig(
new PrebuiltVoiceConfig('Kore'),
)
),
new SpeakerVoiceConfig(
speaker: 'Jane',
voiceConfig: new VoiceConfig(
new PrebuiltVoiceConfig('Puck'),
)
)
]),
languageCode: 'en-GB'
)
)
)->generateContent("TTS the following conversation between Joe and Jane:\nJoe: How's it going today Jane?\nJane: Not too bad, how about you?");
// The response contains base64 encoded audio
$audioData = $response->parts()[0]->inlineData->data;
use Gemini\Data\GenerationConfig;
use Gemini\Data\ThinkingConfig;
$response = $client
->generativeModel(model: 'gemini-2.0-flash-thinking-exp')
->withGenerationConfig(
new GenerationConfig(
thinkingConfig: new ThinkingConfig(
ponse->candidates[0]->content->parts as $part) {
if ($part->thought === true) {
// This part contains the model's thinking process
echo "Model's thinking: " . $part->text . "\n\n";
} else if ($part->text !== null) {
// This is the final answer
echo "Final answer: " . $part->text . "\n";
}
}
$response = $client
->generativeModel(model: 'gemini-2.0-flash')
->countTokens('Write a story about a magic backpack.');
echo $response->totalTokens; // 9
use Gemini\Data\GenerationConfig;
use Gemini\Enums\HarmBlockThreshold;
use Gemini\Data\SafetySetting;
use Gemini\Enums\HarmCategory;
$safetySettingDangerousContent = new SafetySetting(
category: HarmCategory::HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold::BLOCK_ONLY_HIGH
);
$safetySettingHateSpeech = new SafetySetting(
category: HarmCategory::HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold::BLOCK_ONLY_HIGH
);
$generationConfig = new GenerationConfig(
stopSequences: [
'Title',
],
maxOutputTokens: 800,
temperature: 1,
topP: 0.8,
topK: 10
);
$generativeModel = $client
->generativeModel(model: 'gemini-2.0-flash')
->withSafetySetting($safetySettingDangerousContent)
->withSafetySetting($safetySettingHateSpeech)
->withGenerationConfig($generationConfig)
->generateContent('Write a story about a magic backpack.');
$response = $client->files()->list(pageSize: 10);
foreach ($response->files as $file) {
echo "Name: {$file->name}\n";
echo "Display Name: {$file->displayName}\n";
echo "Size: {$file->sizeBytes} bytes\n";
echo "MIME Type: {$file->mimeType}\n";
echo "State: {$file->state->value}\n";
echo "---\n";
}
// Get next page if available
if ($response->nextPageToken) {
$nextPage = $client->files()->list(pageSize: 10, nextPageToken: $response->nextPageToken);
}
$meta = $client->files()->metadataGet('abc123');
// or use the full URI
$meta = $client->files()->metadataGet($file->uri);
echo "File: {$meta->displayName}\n";
echo "State: {$meta->state->value}\n";
echo "Size: {$meta->sizeBytes} bytes\n";
$client->files()->delete('files/abc123');
// or use the full URI
$client->files()->delete($file->uri);
use Gemini\Data\Content;
$cachedContent = $client->cachedContents()->create(
model: 'gemini-2.0-flash',
systemInstruction: Content::parse('You are an expert PHP developer.'),
parts: [
'This is a large codebase...',
'File 1 contents...',
'File 2 contents...'
],
ttl: '3600s', // Cache for 1 hour
displayName: 'PHP Codebase Cache'
);
echo "Cached content created: {$cachedContent->name}\n";
$response = $client
->generativeModel(model: 'gemini-2.0-flash')
->withCachedContent('cachedContents/abc123')
->generateContent('Explain the main function in this codebase');
echo $response->text();
// Check token usage
echo "Cached tokens used: {$response->usageMetadata->cachedContentTokenCount}\n";
echo "New tokens used: {$response->usageMetadata->promptTokenCount}\n";
use Gemini\Enums\FileState;
use Gemini\Enums\MimeType;
use Gemini\Enums\Schema;
use Gemini\Enums\DataType;
$files = $client->files();
echo "Uploading\n";
$meta = $files->upload(
filename: 'document.pdf',
mimeType: MimeType::APPLICATION_PDF,
displayName: 'Document for search'
);
echo "Processing";
do {
echo ".";
sleep(2);
$meta = $files->metadataGet($meta->uri);
} while (! $meta->state->complete());
echo "\n";
if ($meta->state == FileState::Failed) {
die("Upload failed:\n".json_encode($meta->toArray(), JSON_PRETTY_PRINT));
}
$fileSearchStore = $client->fileSearchStores()->create(
displayName: 'My Search Store',
);
echo "File search store created: {$fileSearchStore->name}\n";
use Gemini\Testing\ClientFake;
use Gemini\Responses\GenerativeModel\GenerateContentResponse;
$client = new ClientFake([
GenerateContentResponse::fakeStream(),
]);
$result = $client->generativeModel(model: 'gemini-2.0-flash')->streamGenerateContent('Hello');
expect($response->getIterator()->current())
->text()->toBe('In the bustling city of Aethelwood, where the cobblestone streets whispered');
// assert list models request was sent
use Gemini\Resources\GenerativeModel;
use Gemini\Resources\Models;
$fake->models()->assertSent(callback: function ($method) {
return $method === 'list';
});
// or
$fake->assertSent(resource: Models::class, callback: function ($method) {
return $method === 'list';
});
$fake->geminiPro()->assertSent(function (string $method, array $parameters) {
return $method === 'generateContent' &&
$parameters[0] === 'Hello';
});
// or
$fake->assertSent(resource: GenerativeModel::class, model: 'gemini-2.0-flash', callback: function (string $method, array $parameters) {
return $method === 'generateContent' &&
$parameters[0] === 'Hello';
});
// assert 2 generative model requests were sent
$client->assertSent(resource: GenerativeModel::class, model: 'gemini-2.0-flash', callback: 2);
// or
$client->generativeModel(model: 'gemini-2.0-flash')->assertSent(2);
// assert no generative model requests were sent
$client->assertNotSent(resource: GenerativeModel::class, model: 'gemini-2.0-flash');
// or
$client->generativeModel(model: 'gemini-2.0-flash')->assertNotSent();
// assert no requests were sent
$client->assertNothingSent();
use Gemini\Testing\ClientFake;
use Gemini\Exceptions\ErrorException;
$client = new ClientFake([
new ErrorException([
'message' => 'The model `gemini-basic` does not exist',
'status' => 'INVALID_ARGUMENT',
'code' => 400,
]),
]);
// the `ErrorException` will be thrown
$client->generativeModel(model: 'gemini-2.0-flash')->generateContent('test');
bash
composer
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.