Google ReCaptcha 2 Fatal error: Class 'ReCaptc

2019-09-01 07:57发布

I'm a google recaptcha v2 on a local machine running wamp. Everything looks fine except it keeps dying when its supposed to validate the form

I'm getting this error:

Fatal error: Class 'ReCaptcha\RequestMethod\Post' not found in C:\wamp\www\php\contactForm\Captcha\ReCaptcha.php on line 73

I've pretty much copy/pasted the example code from google:

if (!empty($human)) {
    require_once('Captcha\ReCaptcha.php');
    $recaptcha = new \ReCaptcha\ReCaptcha($secret);
    $resp = $recaptcha->verify($human, $remoteIp);
   if ($resp->isSuccess()) {
    // verified!

I've downloaded the files from the google github (https://github.com/google/recaptcha/tree/master/src/ReCaptcha) and just used the folder /files names as they came. I've got my validation file one folder above but I've also tried coping the files into the same folder as my validation script just in case.

Any ideas?

2条回答
来,给爷笑一个
2楼-- · 2019-09-01 08:20

I hope I don't get slammed to hard but I found if you require all of the files it works as designed without using an autoloader or composer.

//GOOGLE RECAPTCH CODE
require_once('/cgi-bin/src/ReCaptcha/ReCaptcha.php');
require_once('/cgi-bin/src/ReCaptcha/RequestMethod.php');
require_once('/cgi-bin/src/ReCaptcha/RequestParameters.php');
require_once('/cgi-bin/src/ReCaptcha/Response.php');
require_once('/cgi-bin/src/ReCaptcha/RequestMethod/Post.php');
require_once('/cgi-bin/src/ReCaptcha/RequestMethod/Socket.php');
require_once('/cgi-bin/src/ReCaptcha/RequestMethod/SocketPost.php');
$gRecaptchaResponse = $_POST['g-recaptcha-response'];
$secret = 'YOUR SECRET KEY';

$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
    //DO ACTION IF SUCCESSFUL
} else {
    $errors = $resp->getErrorCodes();
}
//END OF GOOGLE RECAPTCHA CODE
查看更多
smile是对你的礼貌
3楼-- · 2019-09-01 08:45

It seems google bank on the fact that everyone use composer to install their repository. And to be honest that is the only installation method they give on thier github repo readme.md https://github.com/google/recaptcha

When you install a package such as google recaptcha with composer the package has the option to register with an autoloader in https://github.com/google/recaptcha/blob/master/composer.json

"autoload": {
    "psr-4": {
        "ReCaptcha\\": "src/ReCaptcha"
    }
},

This way all you have to include in your script to get access to all your package classes is the autoload.php that composer generates when it installs your packages.

Line 34: https://github.com/google/recaptcha/blob/master/examples/example-captcha.php

// Initiate the autoloader.
require_once __DIR__ . '/../vendor/autoload.php';

An autoloader is a function that tries to load a class when php is asked for it. And in this case it kind of maps a namespace to a directory structure on the disk.

More about php autoloaders here: http://php.net/autoload and here: http://www.php-fig.org/psr/psr-4/examples/

If you do not wish to use composer and its autoload functionality, you may find this useful: https://github.com/abraham/twitteroauth it has an autoload.php which you could borrow which can load classes without composer.

  1. Place a copy of it in your recaptcha top level folder (the one with README.md in it)
  2. Replace line 12 with $prefix = 'ReCaptcha\\';
  3. replace line 15 with $base_dir = __DIR__ . '/src/ReCaptcha/';
  4. Require autoloader.php (this file) in your code somewhere
  5. Instantiate your ReCaptcha object something like this in your code new ReCaptcha\ReCaptcha($RECAPTCHASECRETKEY);
查看更多
登录 后发表回答