cryental/wowe-recaptcha
Composer 安装命令:
composer require cryental/wowe-recaptcha
包简介
Implementation of the Google reCAPTCHA API for PHP.
关键字:
README 文档
README
Recaptcha for PHP provides a full-featured PHP implementation of the Google reCAPTCHA API along with some helpful adapters for other popular packages. It specifically uses the "no CAPTCHA" version released in December 2014.
Installation
Via Composer:
composer require wowe/recaptcha:1.*
Configuration
Only two values are required to use the package:
secretThe part of the API key pair used for authentication.siteKeyThe part of the API key pair which uniquely identifies your site.
Both of these values must be acquired from Google. Directions on how to do so may be found here.
Usage
Setup
In order to use the package, create a new instance of the Recaptcha class, passing in the secret and siteKey values.
require 'vendor/autoload.php'; use \Wowe\Recaptcha\Recaptcha; $recaptcha = new Recaptcha('secret', 'siteKey');
Available Methods
script($onload = null, $render = null, $hl = null, $attributes = array())
Generates a script tag based on the options.- $onload
string: The name of the JavaScript function to be called on load. - $render
string: When to render the widget ('explicit' or 'onload'). - $hl
string: The language to be used for the widget. - $attributes
array: Additional attributes for the tag. - Returns
string
- $onload
widget($theme = null, $type = null, $callback = null, $attributes = array())
Generates a div tag for the widget based on the options.- $theme
string: The color theme of the widget ('dark' or 'light'). - $type
string: The type of CAPTCHA to serve ('audio' or 'image'). - $callback
string: The name of the JavaScript callback function to be executed when the user submits a successful CAPTCHA response. - $attributes
array: Additional attributes to be placed on the div. - Returns
string
- $theme
verify($response, $remoteIp = null)
Queries the Google API to determine if the CAPTCHA is valid.- $response
string: The user response token. - $remoteIp
string: The user's IP address. - Returns
boolean
- $response
errors()
The list of errors from the last verification query.- Returns
array
- Returns
Verification Error Codes
After calling verify you may call errors to get a list of any errors that may have been encountered. If any empty array is returned then there were no errors! If there are errors, they will most often be error codes returned by the Google API. In addition to those it may also return:
transfer-error: An exception was encountered when attempting to connect to the API.api-error: A HTTP status code other than 200 was returned by the API.response-error: The format of the response returned by the API could not be read.
Adapters
Twig
Website | GitHub
The script and widget methods can be exposed in Twig templates by adding an instance of the included TwigExtension class to the Twig environment. The TwigExtension instance must be initialized with an instance of the Recaptcha class. script is mapped to a function called recaptchaScript and widget is mapped to a function called recaptchaWidget. All arguments are the same as the definitions above.
// index.php require 'vendor/autoload.php'; use \Wowe\Recaptcha\Recaptcha; use \Wowe\Recaptcha\Adapters\TwigExtension; $recaptcha = new Recaptcha('secret', 'siteKey'); $loader = new Twig_Loader_Filesystem(__DIR__ . '/views'); $twig = new Twig_Environment($loader); $twig->addExtension(new TwigExtension($recaptcha))); echo $twig->render('index.html'); // views/index.html <!doctype html> <html> <head> {{ recaptchaScript() }} </head> <body> <form method="POST"> {{ recaptchaWidget() }} <input type="submit" /> </form> </body> </html>
Slim Framework
Website | GitHub
The Recaptcha class can be registered as a singleton in the Slim container automatically by running the SlimManager::register method, which has three optional arguments:
register($registerViewExtension = false, $recaptcha = null, $appName = null)
Register a Recaptcha instance with the application container.- $registerViewExtension
boolean: Whether or not to also register a view extension (if available). - $recaptcha
\Wowe\Recaptcha\Recaptcha: The Recaptcha instance to bind to. - $appName
string: The name of the application to register with. - Returns void
- $registerViewExtension
If you would like to instantiate the Recaptcha class yourself (in order to get the configuration values customly), you may do so and then just pass it to the register method. If no Recaptcha instance is passed then it will attempt to create one by getting the configuration values from the Slim app. In order to take advantage of this, set the configuration value recaptcha to an array with the secret and siteKey values as in the following example:
require 'vendor/autoload.php'; use \Slim\Slim; use \Wowe\Recaptcha\Adapters\SlimManager; $app = new Slim([ 'recaptcha' => [ 'secret' => 'secret', 'siteKey' => 'siteKey' ] ]); SlimManager::register(); $app->get('/', function () use ($app) { return $app->render('index.html'); }); $app->post('/', function () use ($app) { $recaptchaResponse = $app->request->post('g-recaptcha-response'); var_dump($app->recaptcha->verify($recaptchaResponse), $app->recaptcha->errors()); }); $app->run();
If a value of true is passed as the registerViewExtension and a view engine is being used which has an extension available (eg. Twig), it will register the corresponding extension with the view engine.
Release Notes
Additional information can be found in the CHANGELOG.md file
- v1.0.0 - Initial release
Version Compatibility
| Recaptcha for PHP | Twig | Slim Framework |
|---|---|---|
| v1.x | v1.x | v2.x |
License
The Recaptcha for PHP package is open-sourced software licensed under the MIT license
cryental/wowe-recaptcha 适用场景与选型建议
cryental/wowe-recaptcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「recaptcha」 「google」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cryental/wowe-recaptcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cryental/wowe-recaptcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cryental/wowe-recaptcha 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Client library for reCAPTCHA with proxy support, a free service that protects websites from spam and abuse.
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
A simple and easy to use Google ReCaptcha v3 package for Laravel
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
CakePHP Captcha Plugin, Image, Math and Google Recaptcha Support
ReCaptcha form field integration for Symfony
统计信息
- 总下载量: 30
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-07