Guzzled PHP ReCaptcha
, (*1)
This is a Object Oriented PHP port of the original ReCaptcha lib., (*2)
It's been designed to be testable and uses Guzzle as a
transport layer., (*3)
- see https://developers.google.com/recaptcha/docs/customization
Install
The recommended way to use ReCaptcha is through composer., (*4)
{
"require": {
"neutron/recaptcha": "~0.1.0"
}
}
Silex Service Provider
A simple Silex service provider :, (*5)
use Neutron\ReCaptcha\ReCaptcha;
use Neutron\ReCaptcha\ReCaptchaServiceProvider;
use Silex\Application;
$app = new Application();
$app->register(new ReCaptchaServiceProvider(), array(
'recaptcha.public-key' => 'fdspoksqdpofdkpopgqpdskofpkosd',
'recaptcha.private-key' => 'lsdmkzfqposfomkcqdsofmsdkfkqsdmfmqsdm',
));
// $captcha is an instance of Neutron\ReCaptcha\Response
$captcha = $app['recaptcha']->bind($app['request']);
if ($captcha->isValid()) {
echo "YEAH !";
} else {
echo "Too bad dude :( " . $captcha->getError();
}
Usage Example
To display a captcha to the client :, (*6)
- Initialize your captcha object :
use Neutron\ReCaptcha\ReCaptcha;
$recaptcha = ReCaptcha::create($publicKey, $privateKey);
use Neutron\ReCaptcha\ReCaptcha;
$recaptcha = ReCaptcha::create($publicKey, $privateKey);
$response = $recaptcha->checkAnswer($_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
if ($response->isValid()) {
echo "YEAH !";
} else {
echo "Too bad dude :(";
}
Bind Symfony Request
A shortcut exists to bind a Symfony Request
to ReCaptcha :, (*7)
use Neutron\ReCaptcha\ReCaptcha;
use Symfony\Component\HttpFoundation\Request;
$recaptcha = ReCaptcha::create($publicKey, $privateKey);
$response = $recaptcha->bind(Request::createFromGlobals());
if ($response->isValid()) {
echo "YEAH !";
} else {
echo "Too bad dude :( " . $response->getError();
}
License
This project is licensed under the MIT license., (*8)