Artificial Intelligence 6 min read
How to Use Google Gemini AI in PHP to Solve Image CAPTCHAs
This guide shows how to set up a PHP project, install the Gemini PHP client, and use Google Gemini's multimodal model to recognize text and solve image CAPTCHAs, providing complete code examples, dependency instructions, and sample outputs.
Open Source Tech Hub
Open Source Tech Hub
Introduction
Image CAPTCHAs are designed to block automated tools like web crawlers, but modern multimodal AI models such as Google Gemini can recognize both text and images, making it possible to automate CAPTCHA solving within PHP applications.
Create Project
composer create-project workerman/webman webman20240312Install Dependencies
composer require google-gemini-php/clientGemini PHP is a community‑maintained PHP API client for the Gemini AI service and requires PHP 8.1+.
Project repository: https://github.com/google-gemini-php/client
If your project does not yet use a PSR‑18 client, install the discovery plugin:
composer require php-http/discovery composer require guzzlehttp/guzzleText Model Test
<?php
/**
* @desc Use Google Gemini to generate a text response
*/
declare(strict_types=1);
require_once '../vendor/autoload.php';
$apiKey = 'AIzaSyAPxxxxxxxxxxxxxxx_uEpw';
$client = \Gemini::client($apiKey);
$result = $client->geminiPro()->generateContent('What is PHP?');
echo $result->text() . PHP_EOL;Output example:
PHP (Hypertext Preprocessor) is a general‑purpose scripting language especially suited for web development.
**Features:**
* Open‑source and free
* Cross‑platform
* Easy to learn
* Widely used (WordPress, Facebook, Wikipedia, etc.)
* Modular extensions
* Dynamic typingImage Model Test – Recognize CAPTCHA Text
Get original CAPTCHA image
CAPTCHA file: captcha01.jpg
Reference code
<?php
/**
* @desc Recognize text in a CAPTCHA image using Gemini
*/
declare(strict_types=1);
require_once '../vendor/autoload.php';
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
$apiKey = 'AIzaSyAPLiuNxxxxxxxxxxxxxxx_uEpw';
$client = \Gemini::factory()
->withApiKey($apiKey)
->withBaseUrl('https://gemini.ailard.com/v1/')
->withHttpClient($client = new \GuzzleHttp\Client([]))
->withStreamHandler(fn(RequestInterface $request): ResponseInterface => $client->send($request, ['stream' => true]))
->make();
$result = $client->geminiProVision()->generateContent([
'I will provide you with an image CAPTCHA, please recognize the content inside the CAPTCHA and output the text',
new \Gemini\Data\Blob(
mimeType: \Gemini\Enums\MimeType::IMAGE_JPEG,
data: base64_encode(file_get_contents('./captcha01.jpg'))
)
]);
echo $result->text() . PHP_EOL;Recognition output
The content inside the CAPTCHA is "AXBV".Image Model Test – Compute CAPTCHA Result
Get calculation CAPTCHA image
CAPTCHA file: captcha02.png
Reference code
<?php
/**
* @desc Recognize and compute the result of a calculation CAPTCHA
*/
declare(strict_types=1);
require_once '../vendor/autoload.php';
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
$apiKey = 'AIzaSyAPLiuNxxxxxxxxxxxxxxx_uEpw';
$client = \Gemini::factory()
->withApiKey($apiKey)
->withBaseUrl('https://gemini.ailard.com/v1/')
->withHttpClient($client = new \GuzzleHttp\Client([]))
->withStreamHandler(fn(RequestInterface $request): ResponseInterface => $client->send($request, ['stream' => true]))
->make();
$result = $client->geminiProVision()->generateContent([
'I will provide you with an image CAPTCHA, please recognize the content inside the CAPTCHA and output the text',
new \Gemini\Data\Blob(
mimeType: \Gemini\Enums\MimeType::IMAGE_PNG,
data: base64_encode(file_get_contents('./captcha02.png'))
)
]);
echo $result->text() . PHP_EOL;Recognition output
7 x 8 = ?
56Written by
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
0 followers
Reader feedback
How this landed with the community
Rate this article
Was this worth your time?
Discussion
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
