PHPackages                             nsu-soft/yii-cap-captcha - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. nsu-soft/yii-cap-captcha

ActiveLibrary[Security](/categories/security)

nsu-soft/yii-cap-captcha
========================

Interface for accessing a stand-alone server with Cap Captcha

3.2(2mo ago)091BSD-3-ClausePHPPHP &gt;=8.3

Since Mar 13Pushed 2mo agoCompare

[ Source](https://github.com/nsu-soft/yii-cap-captcha)[ Packagist](https://packagist.org/packages/nsu-soft/yii-cap-captcha)[ RSS](/packages/nsu-soft-yii-cap-captcha/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (4)Dependencies (26)Versions (10)Used By (1)

yii-cap-captcha
===============

[](#yii-cap-captcha)

[На русском](docs/ru-RU/README.md)

[![License](https://camo.githubusercontent.com/6cb285b57819f8de0acfb34923298f4f569f962544e8fe35331da2d163f4e485/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4253442d2d332d2d436c617573652d626c75652e737667)](LICENSE)[![PHP Version](https://camo.githubusercontent.com/82f2ea35cf5859a37ddb09e749933f856efbb2ca9be0231de6ad6e0abbfcc8c3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e332d3838393242462e7376673f6c6f676f3d706870)](https://php.net)[![Yii Version](https://camo.githubusercontent.com/bcc4a3ac99a3802e6dd12cdbe5a679645ad218f297eca34fb221736851ceae6c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7969692d7e322e302e35302d4534374234342e7376673f6c6f676f3d796969)](https://www.yiiframework.com)[![Status](https://camo.githubusercontent.com/986e45346faab8c994df0c11d6060aa0e540f804c47d09fd23625a3390a0f0b8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f737461626c652d332e322d626c75652e737667)](https://packagist.org/packages/nsu-soft/yii-cap-captcha)

---

📑 Table of Contents
-------------------

[](#-table-of-contents)

- [Overview](#-overview)
- [Features](#-features)
- [Requirements](#-requirements)
- [Installation](#-installation)
- [Configuration](#-configuration)
- [Usage](#-usage)
- [Testing](#-testing)
- [Project Structure](#-project-structure)
- [Error Handling](#-error-handling)
- [Security](#-security)
- [Contributing](#-contributing)
- [License](#-license)

---

📋 Overview
----------

[](#-overview)

**yii-cap-captcha** is a PHP library for the **Yii2** framework that provides a robust and convenient interface for interacting with the standalone **[Cap Captcha](https://github.com/tiagozip/cap)** server. The package enables seamless integration of bot protection into your Yii2 applications by leveraging an external service for captcha generation, challenge delivery, and response validation.

This component follows Yii2 design patterns and is fully compatible with PSR-7 (HTTP message) and PSR-18 (HTTP client) standards.

---

✨ Features
----------

[](#-features)

- 🔐 **Full Cap Captcha API Integration**: Access all endpoints including challenge, redeem, siteverify, and administrative operations.
- 🧩 **Yii2 Component Architecture**: Drop-in component with support for Yii's application configuration.
- 🔑 **Comprehensive Key Management**: Create, view, update, rotate, and delete site keys and API keys programmatically.
- 🐳 **Docker-Ready**: Pre-configured `docker-compose.yml` for instant local development and testing environments.
- 🧪 **Tested with Codeception**: Full test suite covering unit and functional scenarios.
- 📦 **PSR Compliant**: Built on PSR-7 HTTP messages and PSR-18 HTTP client interfaces for maximum interoperability.
- 🔄 **Flexible HTTP Client**: Works with any PSR-18 compatible HTTP client via discovery or explicit configuration.

---

🔧 Requirements
--------------

[](#-requirements)

ComponentVersionDescription**PHP**`>= 8.3`Required PHP version**Yii Framework**`~2.0.50`Yii2 web application framework**PSR HTTP Client**`^1.0`HTTP client interface (PSR-18)**HTTP Message Factory**`^1.0`PSR-7 message factory interface**Cap Captcha Server**`^2.2`Cap Captcha standalone server---

📦 Installation
--------------

[](#-installation)

If you don't have Composer, you may install it by [following instruction](https://getcomposer.org/doc/00-intro.md).

Than install the package via Composer:

```
composer require nsu-soft/yii-cap-captcha
```

You also need an HTTP client installed in your project. You may use standard [yiisoft/yii2-httpclient](https://packagist.org/packages/yiisoft/yii2-httpclient). Additionally you need to install PSR-17 implementation. For example:

```
composer require yiisoft/yii2-httpclient
composer require nyholm/psr7
```

Or you may use one of [PSR-18 implementations](https://packagist.org/providers/psr/http-client-implementation).

For example:

```
composer require guzzlehttp/guzzle
```

The package will use the HTTP client that it finds in the project. It uses [php-http/discovery](https://packagist.org/packages/php-http/discovery) package to find the HTTP client.

---

⚙️ Configuration
----------------

[](#️-configuration)

### Step 1: Generate keys in Cap Captcha server admin panel

[](#step-1-generate-keys-in-cap-captcha-server-admin-panel)

Generate access keys to the Cap Captcha server as described on the [official website](https://capjs.js.org/guide/) in the "Setting up your server" section.

### Step 2: Add configuration to your Yii 2 application

[](#step-2-add-configuration-to-your-yii-2-application)

Add your Cap Captcha server details to your application configuration:

```
    'components' => [
        'captcha' => [
            'class' => NsuSoft\Captcha\Cap::class,

            'baseUri' => 'http://localhost:3000', // Base URI of the Cap server

            // If base URI from server and client sides are different,
            // you can specify it separately
            // 'baseUri' => [
            //     'server' => 'http://cap:3000', // To using in Docker
            //     'client' => 'http://localhost:3000', // To using in widget
            // ],

            // Site credentials (provided during site registration)
            'siteKey' => 'your-site-key',          // Public site identifier
            'secretKey' => 'your-secret-key',      // Private key for client request validation

            // Optional: API key for administrative operations
            'apiKey' => '',                        // Leave empty if not managing keys programmatically
        ],
    ],
```

---

💻 Usage
-------

[](#-usage)

### Getting Component Instance

[](#getting-component-instance)

Access the configured component from anywhere in your Yii2 application:

```
$cap = Yii::$app->captcha;
```

### Methods Reference

[](#methods-reference)

JSON schemas are located in `tests/Support/Data/Cap` directory.

MethodDescriptionParametersReturns`getEndpoint()`Gets `endpoint` property for `NsuSoft\Captcha\CapWidget`—`string`. Cap Captcha API endpoint like `http:///``challenge()`Gets CAPTCHA challenge—See JSON schema in `Main/challenge.200.json``redeem(string $token, array $solutions)`Redeem a solution to token`$token`: token from `/{siteKey}/challenge` endpoint, `$solutions`: CAPTCHA solutionsSee JSON schema in `Main/redeem.200.json``siteVerify(string $response)`Validation endpoint`$response'`: token from `/{siteKey}/redeem` endpointSee JSON schema in `Main/siteverify.200.json``getAbout()`Retrieves server metadata—See JSON schema in `Server/about.200.json``logout(string $session)`Logout specified session`$session``null``getKeys()`Gets all sites keys—See JSON schema in `Server/Keys/index.200.json``createKey(string $name)`Creates site key`$name`See JSON schema in `Server/Keys/post.200.json``viewKey(string $siteKey)`Views site key`$siteKey`See JSON schema in `Server/Keys/get.200.json``deleteKey(string $siteKey)`Deletes site key with it secret`$siteKey`See JSON schema in `Server/Keys/delete.200.json``configKey(string $siteKey, array $options = [])`Configures site key`$siteKey`, `$options = ['challengeCount' => int, 'difficulty' => int, 'name' => 'new-name', 'saltSize' => int]`See JSON schema in `Server/Keys/config.200.json``rotateSecret(string $siteKey)`Rotates secret key for specified site key—See JSON schema in `Server/Keys/rotate-secret.200.json``getApiKeys()`Gets all API keys—See JSON schema in `Server/Settings/apikeys.index.200.json``createApiKey(string $name)`Creates API key`$name`See JSON schema in `Server/Settings/apikeys.post.200.json``deleteApiKey(string $id)`Deletes API key`$id`See JSON schema in `Server/Settings/apikeys.delete.200.json``deleteLastApiKey(string $name)`Deletes a last added API key by it name`$name`See JSON schema in `Server/Settings/apikeys.delete.200.json``getSessions()`Gets all sessions tokens—See JSON schema in `Server/Settings/sessions.200.json`### Example: Validating User Response in Controller

[](#example-validating-user-response-in-controller)

```
