dario_swain / re-captcha-library
Google ReCAPTCHA (ver. 2) 后端提供者。
2.0.0
2016-01-08 22:16 UTC
Requires
- php: >=5.3.3
- psr/http-message: ~1.0
Requires (Dev)
- phpunit/phpunit: 4.8.*
- squizlabs/php_codesniffer: 2.5.*
- symfony/yaml: 2.8.*
Suggests
- guzzlehttp/guzzle: Advanced PHP HTTP client.
This package is not auto-updated.
Last update: 2024-09-24 04:04:50 UTC
README
#Google ReCAPTCHA ver.2 后端客户端
您可以在这里找到关于Google reCAPTCHA API v2的完整文档。
##安装
您可以使用Composer安装此包。在您的composer.json文件中添加以下行
{ "require": { "dario_swain/re-captcha-library": "2.0.*" } }
或者您可以使用composer require
,如下所示
composer require dario_swain/re-captcha-library 2.0.*
##使用示例
####显示小部件
<html> <head> <script src='https://www.google.com/recaptcha/api.js'></script> </head> <body> <form method="post"> <div class="g-recaptcha" data-sitekey="{RECAPTCHA SITE KEY}"></div> <br> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
关于客户端集成的更多信息,请参阅官方文档。
####验证用户的响应
<?php $privateKey = 'RECAPTCHA PRIVATE KEY'; //You Google API private key $clientIp = $_SERVER['REMOTE_ADDR']; //Client IP Address $gReCaptchaResponse = $_POST['g-recaptcha-response']; //Google reCAPTCHA response $reCaptchaClient = new Client($privateKey); try { $success = $reCaptchaClient->validate($gReCaptchaResponse, $clientIp); if ($success) { //Submit form } } catch(ValidationException $e) { $validationError = $e->getMessage(); }
在examples/index.php
中可以找到简单的示例。
##自定义客户端
您可以将reCaptcha标准HTTP客户端更改为自定义客户端实现。在这种情况下,您可以使用DS\Library\ReCaptcha\Http\Client\ClientInterface
对象。您还可以使用任何PSR7兼容的HTTP客户端,如下所示
<?php class ProxyClient implements ClientInterface { {@inheritdoc} public function send(RequestInterface $request); { //Your business logic } } ... $proxyHttpClient = new ProxyClient(); $reCaptchaClient = new Client($privateKey, $proxyHttpClient); $reCaptchaClient->validate($gReCaptchaResponse, $clientIp);
##Guzzle集成
您可以使用更高级的HTTP客户端(如Guzzle)而不是标准HTTP客户端。现在ReCaptchaLibrary支持3.*
、4.*
、5.*
和6.*
版本的guzzlehttp/guzzle
Guzzle客户端示例
use DS\Library\ReCaptcha\Http\Client\Guzzle\GuzzleClient; $reCaptchaGuzzleClient = new GuzzleClient(); //Guzzle client will be detected automatically //Also you can manually create and initialize Guzzle Client $guzzle = new \GuzzleHttp\Client($configuration); $reCaptchaGuzzleClient = new GuzzleClient($guzzle); $reCaptchaClient = new Client('PRIVATE KEY', $reCaptchaGuzzleClient); $reCaptchaClient->validate($gResponse);
#版权
版权所有 (c) 2015 Ilya Pokamestov dario_swain@yahoo.com。