google-gemini-php/client
⚡️ Gemini PHP is a community-maintained PHP API client that allows you to interact with the Gemini AI API.
Overview
google‑gemini‑php/client is a community‑maintained PHP library that wraps Google’s Gemini generative AI API. It lets PHP developers call Gemini models for text‑completion, multimodal (text + image/video) queries, image generation, streaming responses, structured JSON output, function calling, code execution, and various “grounding” features such as live Google Search, Maps and file‑search. The package is distributed via Composer and requires PHP 8.2+.
Key Features
| Feature | What it does |
|---|---|
| Model access | Call any Gemini model (e.g., gemini-2.0-flash, gemini-2.5-flash-image) via $client->generativeModel() |
| Multimodal input | Send text together with images (Blob) or videos (UploadedFile) in a single request |
| Image generation | Use the Imagen model to create pictures from prompts, with configurable aspect ratios |
| Chat & streaming | Build multi‑turn conversations; optionally receive partial results as a stream |
| Structured output | Request JSON (or other MIME types) and define a response schema so the model returns machine‑readable data |
| Function calling | Declare functions the model may invoke, receive a FunctionCall object, run the function, and feed the result back |
| Code execution | Enable the model to generate and run code, returning the execution result |
| Grounding tools | Attach Google Search, Google Maps, or private File‑Search stores so the model can cite up‑to‑date or private information |
| System instructions | Set a system‑level prompt that influences the model’s tone, style, or behavior |
| File management & cached content | Upload, list, delete files; create cached content objects for faster repeated queries |
| Configuration | Customize base URL, HTTP headers, query parameters, HTTP client, and stream handler |
Typical Usage Flow
- Install via Composer:
composer require google-gemini-php/client - Provide an API key from Google AI Studio.
- Create a client – either the quick
Gemini::client($key)helper or a fully‑configured factory. - Choose a model with
$client->generativeModel('gemini-2.0-flash'). - Call a method such as
generateContent(),streamGenerateContent(), or start a chat withstartChat(). - Handle the response – call
$result->text(),$result->json(), or inspect parts for function calls, code execution results, etc.
Example Snippet (Chat with System Prompt)
use Gemini; // façade provided by the package
$client = Gemini::client(getenv('GEMINI_API_KEY'));
$response = $client
->generativeModel('gemini-2.0-flash')
->withSystemInstruction(
Gemini\Data\Content::parse(
'You are a helpful assistant that always responds in the style of a pirate.'
)
)
->generateContent('Tell me about PHP programming');
echo $response->text(); // Ahoy there, matey! …
Who It’s For
- PHP developers who want to add generative AI capabilities to web apps, CLI tools, or backend services.
- Teams needing multimodal (text + image/video) or real‑time (streaming) interactions.
- Projects that require structured data (JSON) or tool use (function calling, code execution, live search).
Limitations & Considerations
- The library only supports the Gemini v1beta API (as of version 2.0). Older Gemini versions are not compatible.
- You must supply your own Google API key and handle any associated billing.
- Some advanced features (e.g., function‑calling signatures, code execution) depend on specific Gemini model versions; check the official Gemini docs for model compatibility.
- The package follows PSR‑18 HTTP client standards; you need a compatible HTTP client (Guzzle is recommended).
All information above is taken directly from the repository’s README; no additional features have been inferred.
Related
- Project
- Project
- Project
- Project