shevabam/simple-hcaptcha 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shevabam/simple-hcaptcha

最新稳定版本:v1.0.0

Composer 安装命令:

composer require shevabam/simple-hcaptcha

包简介

A simple PHP library to integrate hCaptcha in your PHP applications

README 文档

README

A simple PHP library to integrate hCaptcha in your PHP applications.

Requirements

  • PHP 7.4 or higher
  • cURL extension
  • JSON extension

Installation

Install via Composer:

composer require shevabam/hcaptcha-php

Usage

Basic Usage

<?php
require 'vendor/autoload.php';

use SimpleHcaptcha\Hcaptcha;

// Initialize with your site key and secret key
$hcaptcha = new Hcaptcha('your-site-key', 'your-secret-key');

// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $hcaptchaResponse = $_POST['h-captcha-response'] ?? '';
    
    // Remote IP is optional, but recommended
    if ($hcaptcha->isValid($hcaptchaResponse, $_SERVER['REMOTE_ADDR'])) {
        // CAPTCHA validation passed, process the form
        echo "Form submitted successfully!";
    } else {
        // CAPTCHA validation failed
        echo "CAPTCHA verification failed: " . $hcaptcha->getError();
    }
}
?>

<!-- Your HTML form -->
<html>
<head>
    <?php echo $hcaptcha->getScript(); ?>
</head>
<body>
    <form method="post">
        <!-- Your form fields -->
        
        <!-- Display hCaptcha widget -->
        <?php echo $hcaptcha->display(); ?>
        
        <button type="submit">Submit</button>
    </form>
</body>
</html>

Available Methods

getScript(): string

Generate the HTML code to include the hCaptcha script.

display(): string

Generate the HTML code to display the hCaptcha widget.

isValid(string $response, ?string $remoteIp = null): bool

Verify if the CAPTCHA response is valid.

getError(): string

Get the error message of the last operation.

getErrorCode(): ?string

Get the error code of the last operation.

setTheme(string $theme): self

Set the theme of the hCaptcha widget ('light' or 'dark').

setSize(string $size): self

Set the size of the hCaptcha widget ('normal' or 'compact').

setLanguage(string $language): self

Set the language of the hCaptcha widget.

setOption(string $key, $value): self

Set a custom option.

Configuration

You can configure the hCaptcha widget by passing an array of options to the constructor or using the setter methods:

$hcaptcha = new Hcaptcha('your-site-key', 'your-secret-key', [
    'theme' => 'dark',
    'size' => 'normal',
    'language' => 'fr'
]);

// Or using setter methods
$hcaptcha
    ->setTheme('dark')
    ->setSize('normal')
    ->setLanguage('fr');

Usage

To generate the script tag, use :

$hcaptcha->getScript();

To display the widget in your form, use :

$hcaptcha->display();

To verify the CAPTCHA response, use :

if ($hcaptcha->isValid($_POST['h-captcha-response'], $_SERVER['REMOTE_ADDR'])) {
    // CAPTCHA validation passed, process the form
    echo "Form submitted successfully!";
} else {
    // CAPTCHA validation failed
    echo "CAPTCHA verification failed: " . $hcaptcha->getError();
}

Error Handling

When isValid() returns false, you can get the error message using getError():

if (!$hcaptcha->isValid($response)) {
    $error = $hcaptcha->getError();
    // Handle the error
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Created by @shevabam

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-20