{"id":13628220,"url":"https://github.com/gemini-api-php/client","last_synced_at":"2025-05-15T15:01:50.859Z","repository":{"id":213215647,"uuid":"733342648","full_name":"gemini-api-php/client","owner":"gemini-api-php","description":"Google Gemini API PHP Client allows you to use the Gemini AI model","archived":false,"fork":false,"pushed_at":"2025-01-31T15:33:12.000Z","size":1198,"stargazers_count":178,"open_issues_count":18,"forks_count":42,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-05-12T16:15:31.594Z","etag":null,"topics":["client","gemini","gemini-api","google-gemini","google-generative-ai","php","sdk"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gemini-api-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-19T05:40:06.000Z","updated_at":"2025-05-03T08:07:14.000Z","dependencies_parsed_at":"2023-12-24T00:02:43.205Z","dependency_job_id":"faa2d8cc-c00f-40c5-8bed-db93afeb3a71","html_url":"https://github.com/gemini-api-php/client","commit_stats":null,"previous_names":["erdemkose/generative-ai-php","gemini-api-php/client"],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gemini-api-php%2Fclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gemini-api-php%2Fclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gemini-api-php%2Fclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gemini-api-php%2Fclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gemini-api-php","download_url":"https://codeload.github.com/gemini-api-php/client/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254364264,"owners_count":22058877,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["client","gemini","gemini-api","google-gemini","google-generative-ai","php","sdk"],"created_at":"2024-08-01T22:00:48.634Z","updated_at":"2025-05-15T15:01:50.764Z","avatar_url":"https://github.com/gemini-api-php.png","language":"PHP","funding_links":[],"categories":["GitHub projects"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/gemini-api-php/client/main/assets/example.png\" width=\"800\" alt=\"Gemini API PHP Client - Example\"\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://packagist.org/packages/gemini-api-php/client\"\u003e\u003cimg alt=\"Total Downloads\" src=\"https://img.shields.io/packagist/dt/gemini-api-php/client\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://packagist.org/packages/gemini-api-php/client\"\u003e\u003cimg alt=\"Latest Version\" src=\"https://img.shields.io/packagist/v/gemini-api-php/client\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://packagist.org/packages/gemini-api-php/client\"\u003e\u003cimg alt=\"License\" src=\"https://img.shields.io/github/license/gemini-api-php/client\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# Gemini API PHP Client\n\nGemini API PHP Client allows you to use the Google's generative AI models, like Gemini Pro and Gemini Pro Vision.\n\n_This library is not developed or endorsed by Google._\n\n- Erdem Köse - **[github.com/erdemkose](https://github.com/erdemkose)**\n\n## Table of Contents\n- [Installation](#installation)\n- [How to use](#how-to-use)\n  - [Basic text generation](#basic-text-generation)\n  - [Text generation with system instruction](#text-generation-with-system-instruction)\n  - [Multimodal input](#multimodal-input)\n  - [Chat Session (Multi-Turn Conversations)](#chat-session-multi-turn-conversations)\n  - [Chat Session with history](#chat-session-with-history)\n  - [Streaming responses](#streaming-responses)\n  - [Streaming Chat Session](#streaming-chat-session)\n  - [Tokens counting](#tokens-counting)\n  - [Listing models](#listing-models)\n  - [Advanced Usages](#advanced-usages)\n    - [Using Beta version](#using-beta-version)\n    - [Safety Settings and Generation Configuration](#safety-settings-and-generation-configuration)\n    - [Using your own HTTP client](#using-your-own-http-client)\n    - [Using your own HTTP client for streaming responses](#using-your-own-http-client-for-streaming-responses)\n\n## Installation\n\n\u003e You need an API key to gain access to Google's Gemini API.\n\u003e Visit [Google AI Studio](https://makersuite.google.com/) to get an API key.\n\nFirst step is to install the Gemini API PHP client with Composer.\n\n```shell\ncomposer require gemini-api-php/client\n```\n\nGemini API PHP client does not come with an HTTP client.\nIf you are just testing or do not have an HTTP client library in your project,\nyou need to allow `php-http/discovery` composer plugin or install a PSR-18 compatible client library.\n\n## How to use\n\n### Basic text generation\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\n\n$client = new Client('GEMINI_API_KEY');\n$response = $client-\u003egenerativeModel(ModelName::GEMINI_PRO)-\u003egenerateContent(\n    new TextPart('PHP in less than 100 chars'),\n);\n\nprint $response-\u003etext();\n// PHP: A server-side scripting language used to create dynamic web applications.\n// Easy to learn, widely used, and open-source.\n```\n\n### Text generation with system instruction\n\n\u003e System instruction is currently supported only in beta version\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\n\n$client = new Client('GEMINI_API_KEY');\n$response = $client-\u003ewithV1BetaVersion()\n    -\u003egenerativeModel(ModelName::GEMINI_1_5_FLASH)\n    -\u003ewithSystemInstruction('You are a cat. Your name is Neko.')\n    -\u003egenerateContent(\n        new TextPart('PHP in less than 100 chars'),\n    );\n\nprint $response-\u003etext();\n// Meow?  \u003c?php echo 'Hello, world!'; ?\u003e  Purrfectly concise, wouldn't you say?  *Stretches luxuriously*\n```\n\n### Multimodal input\n\n\u003e Image input modality is only enabled for Gemini Pro Vision model\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Enums\\MimeType;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\ImagePart;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\n\n$client = new Client('GEMINI_API_KEY');\n$response = $client-\u003egenerativeModel(ModelName::GEMINI_PRO)-\u003egenerateContent(\n    new TextPart('Explain what is in the image'),\n    new ImagePart(\n        MimeType::IMAGE_JPEG,\n        base64_encode(file_get_contents('elephpant.jpg')),\n    ),\n);\n\nprint $response-\u003etext();\n// The image shows an elephant standing on the Earth.\n// The elephant is made of metal and has a glowing symbol on its forehead.\n// The Earth is surrounded by a network of glowing lines.\n// The image is set against a starry background.\n```\n\n### Chat Session (Multi-Turn Conversations)\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\n\n$client = new Client('GEMINI_API_KEY');\n$chat = $client-\u003egenerativeModel(ModelName::GEMINI_PRO)-\u003estartChat();\n\n$response = $chat-\u003esendMessage(new TextPart('Hello World in PHP'));\nprint $response-\u003etext();\n\n$response = $chat-\u003esendMessage(new TextPart('in Go'));\nprint $response-\u003etext();\n```\n\n```text\n\u003c?php\necho \"Hello World!\";\n?\u003e\n\nThis code will print \"Hello World!\" to the standard output.\n```\n\n```text\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Println(\"Hello World!\")\n}\n\nThis code will print \"Hello World!\" to the standard output.\n```\n\n### Chat Session with history\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Enums\\Role;\nuse GeminiAPI\\Resources\\Content;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\n\n$history = [\n    Content::text('Hello World in PHP', Role::User),\n    Content::text(\n        \u003c\u003c\u003cTEXT\n        \u003c?php\n        echo \"Hello World!\";\n        ?\u003e\n        \n        This code will print \"Hello World!\" to the standard output.\n        TEXT,\n        Role::Model,\n    ),\n];\n\n$client = new Client('GEMINI_API_KEY');\n$chat = $client-\u003egenerativeModel(ModelName::GEMINI_PRO)\n    -\u003estartChat()\n    -\u003ewithHistory($history);\n\n$response = $chat-\u003esendMessage(new TextPart('in Go'));\nprint $response-\u003etext();\n```\n\n```text\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Println(\"Hello World!\")\n}\n\nThis code will print \"Hello World!\" to the standard output.\n```\n\n### Streaming responses\n\n\u003e Requires `curl` extension to be enabled\n\nIn the streaming response, the callback function will be called whenever a response is returned from the server.\n\nLong responses may be broken into separate responses, and you can start receiving responses faster using a content stream.\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\nuse GeminiAPI\\Responses\\GenerateContentResponse;\n\n$callback = function (GenerateContentResponse $response): void {\n    static $count = 0;\n\n    print \"\\nResponse #{$count}\\n\";\n    print $response-\u003etext();\n    $count++;\n};\n\n$client = new Client('GEMINI_API_KEY');\n$client-\u003egenerativeModel(ModelName::GEMINI_PRO)-\u003egenerateContentStream(\n    $callback,\n    [new TextPart('PHP in less than 100 chars')],\n);\n// Response #0\n// PHP: a versatile, general-purpose scripting language for web development, popular for\n// Response #1\n//  its simple syntax and rich library of functions.\n```\n\n### Streaming Chat Session\n\n\u003e Requires `curl` extension to be enabled \n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Enums\\Role;\nuse GeminiAPI\\Resources\\Content;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\nuse GeminiAPI\\Responses\\GenerateContentResponse;\n\n$history = [\n    Content::text('Hello World in PHP', Role::User),\n    Content::text(\n        \u003c\u003c\u003cTEXT\n        \u003c?php\n        echo \"Hello World!\";\n        ?\u003e\n        \n        This code will print \"Hello World!\" to the standard output.\n        TEXT,\n        Role::Model,\n    ),\n];\n\n$callback = function (GenerateContentResponse $response): void {\n    static $count = 0;\n\n    print \"\\nResponse #{$count}\\n\";\n    print $response-\u003etext();\n    $count++;\n};\n\n$client = new Client('GEMINI_API_KEY');\n$chat = $client-\u003egenerativeModel(ModelName::GEMINI_PRO)\n    -\u003estartChat()\n    -\u003ewithHistory($history);\n\n$chat-\u003esendMessageStream($callback, new TextPart('in Go'));\n```\n\n```text\nResponse #0\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\nResponse #1\n    fmt.Println(\"Hello World!\")\n}\n\nThis code will print \"Hello World!\" to the standard output.\n```\n\n### Embed Content\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\n\n$client = new Client('GEMINI_API_KEY');\n$response = $client-\u003eembeddingModel(ModelName::EMBEDDING_001)\n    -\u003eembedContent(\n        new TextPart('PHP in less than 100 chars'),\n    );\n\nprint_r($response-\u003eembedding-\u003evalues);\n// [\n//    [0] =\u003e 0.041395925\n//    [1] =\u003e -0.017692696\n//    ...\n// ]\n```\n\n### Tokens counting\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\n\n$client = new Client('GEMINI_API_KEY');\n$response = $client-\u003egenerativeModel(ModelName::GEMINI_PRO)-\u003ecountTokens(\n    new TextPart('PHP in less than 100 chars'),\n);\n\nprint $response-\u003etotalTokens;\n// 10\n```\n\n### Listing models\n\n```php\nuse GeminiAPI\\Client;\n\n$client = new Client('GEMINI_API_KEY');\n$response = $client-\u003elistModels();\n\nprint_r($response-\u003emodels);\n//[\n//  [0] =\u003e GeminiAPI\\Resources\\Model Object\n//    (\n//      [name] =\u003e models/gemini-pro\n//      [displayName] =\u003e Gemini Pro\n//      [description] =\u003e The best model for scaling across a wide range of tasks\n//      ...\n//    )\n//  [1] =\u003e GeminiAPI\\Resources\\Model Object\n//    (\n//      [name] =\u003e models/gemini-pro-vision\n//      [displayName] =\u003e Gemini Pro Vision\n//      [description] =\u003e The best image understanding model to handle a broad range of applications\n//      ...\n//    )\n//]\n```\n\n### Advanced Usages\n\n#### Using Beta version\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\n\n$client = (new Client('GEMINI_API_KEY'))\n    -\u003ewithV1BetaVersion();\n$response = $client-\u003egenerativeModel(ModelName::GEMINI_PRO)-\u003ecountTokens(\n    new TextPart('PHP in less than 100 chars'),\n);\n\nprint $response-\u003etotalTokens;\n// 10\n```\n\n#### Safety Settings and Generation Configuration\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Enums\\HarmCategory;\nuse GeminiAPI\\Enums\\HarmBlockThreshold;\nuse GeminiAPI\\GenerationConfig;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\nuse GeminiAPI\\SafetySetting;\n\n$safetySetting = new SafetySetting(\n    HarmCategory::HARM_CATEGORY_HATE_SPEECH,\n    HarmBlockThreshold::BLOCK_LOW_AND_ABOVE,\n);\n$generationConfig = (new GenerationConfig())\n    -\u003ewithCandidateCount(1)\n    -\u003ewithMaxOutputTokens(40)\n    -\u003ewithTemperature(0.5)\n    -\u003ewithTopK(40)\n    -\u003ewithTopP(0.6)\n    -\u003ewithStopSequences(['STOP']);\n\n$client = new Client('GEMINI_API_KEY');\n$response = $client-\u003egenerativeModel(ModelName::GEMINI_PRO)\n    -\u003ewithAddedSafetySetting($safetySetting)\n    -\u003ewithGenerationConfig($generationConfig)\n    -\u003egenerateContent(\n        new TextPart('PHP in less than 100 chars')\n    );\n```\n\n#### Using your own HTTP client\n\n```php\nuse GeminiAPI\\Client as GeminiClient;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\nuse GuzzleHttp\\Client as GuzzleClient;\n\n$guzzle = new GuzzleClient([\n  'proxy' =\u003e 'http://localhost:8125',\n]);\n\n$client = new GeminiClient('GEMINI_API_KEY', $guzzle);\n$response = $client-\u003egenerativeModel(ModelName::GEMINI_PRO)-\u003egenerateContent(\n    new TextPart('PHP in less than 100 chars')\n);\n```\n\n#### Using your own HTTP client for streaming responses\n\n\u003e Requires `curl` extension to be enabled\n\nSince streaming responses are fetched using `curl` extension, they cannot use the custom HTTP client passed to the Gemini Client.\nYou need to pass a `CurlHandler` if you want to override connection options.\n\nThe following curl options will be overwritten by the Gemini Client.\n\n- `CURLOPT_URL`\n- `CURLOPT_POST`\n- `CURLOPT_POSTFIELDS`\n- `CURLOPT_WRITEFUNCTION`\n\nYou can also pass the headers you want to be used in the requests.\n\n```php\nuse GeminiAPI\\Client;\nuse GeminiAPI\\Resources\\ModelName;\nuse GeminiAPI\\Resources\\Parts\\TextPart;\nuse GeminiAPI\\Responses\\GenerateContentResponse;\n\n$callback = function (GenerateContentResponse $response): void {\n    print $response-\u003etext();\n};\n\n$ch = curl_init();\ncurl_setopt($ch, \\CURLOPT_PROXY, 'http://localhost:8125');\n\n$client = new Client('GEMINI_API_KEY');\n$client-\u003ewithRequestHeaders([\n        'User-Agent' =\u003e 'My Gemini-backed app'\n    ])\n    -\u003egenerativeModel(ModelName::GEMINI_PRO)\n    -\u003egenerateContentStream(\n        $callback,\n        [new TextPart('PHP in less than 100 chars')],\n        $ch,\n    );\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgemini-api-php%2Fclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgemini-api-php%2Fclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgemini-api-php%2Fclient/lists"}