Skip to content
Advertisement

Google ReCaptcha 2 Fatal error: Class ‘ReCaptchaRequestMethodPost’ not found

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 'ReCaptchaRequestMethodPost' not found in C:wampwwwphpcontactFormCaptchaReCaptcha.php on line 73

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

if (!empty($human)) {
    require_once('CaptchaReCaptcha.php');
    $recaptcha = new ReCaptchaReCaptcha($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?

Advertisement

Answer

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 ReCaptchaReCaptcha($RECAPTCHASECRETKEY);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement