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
Open Source Tech Hub
How to Use Google Gemini AI in PHP to Solve Image CAPTCHAs

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 webman20240312

Install Dependencies

composer require google-gemini-php/client
Gemini 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/guzzle

Text 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 typing

Image 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 = ?
56
Artificial IntelligenceCaptchaPHPImage RecognitionGemini AI
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.