Google ReCaptcha 2 致命错误:找不到“ReCaptcha\RequestMethod\Post"类 [英] Google ReCaptcha 2 Fatal error: Class 'ReCaptcha\RequestMethod\Post' not found

查看:129
本文介绍了Google ReCaptcha 2 致命错误:找不到“ReCaptcha\RequestMethod\Post"类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是运行 wamp 的本地机器上的 google recaptcha v2.一切看起来都很好,只是在它应该验证表单时它一直在死亡

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

我收到此错误:

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!

我已经从 google github (https://github.com/google/recaptcha/tree/master/src/ReCaptcha) 并使用文件夹/files 来时的名称.我的验证文件位于上面的一个文件夹中,但我也尝试将这些文件复制到与验证脚本相同的文件夹中,以防万一.

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.

有什么想法吗?

推荐答案

似乎每个人都使用 Composer 来安装他们的存储库的事实 Google 银行.老实说,这是他们在 github 存储库 readme.md 上提供的唯一安装方法 https://github.com/谷歌/重新验证码

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

当您使用 composer 安装诸如 google recaptcha 之类的软件包时,该软件包可以选择在 https://github.com/google/recaptcha/blob/master/composer.json

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"
    }
},

通过这种方式,为了访问所有包类,您必须在脚本中包含的就是 composer 在安装包时生成的 autoload.php.

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.

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

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

自动加载器是一个函数,当 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.

更多关于 php 自动加载器的信息,请访问:http://php.net/autoload 和这里:http://www.php-fig.org/psr/psr-4/examples/

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

如果您不想使用 composer 及其自动加载功能,您可能会发现这很有用:https://github.com/abraham/twitteroauth 它有一个 autoload.php,你可以借用它可以在没有作曲家的情况下加载类.

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. 将其副本放在您的 recaptcha 顶级文件夹中(其中包含 README.md 的文件夹)
  2. 将第 12 行替换为 $prefix = 'ReCaptcha\\';
  3. $base_dir = __DIR__ 替换第 15 行.'/src/ReCaptcha/';
  4. 在代码中的某处需要 autoloader.php(此文件)
  5. 在你的代码中实例化你的 ReCaptcha 对象new ReCaptcha\ReCaptcha($RECAPTCHASECRETKEY);

这篇关于Google ReCaptcha 2 致命错误:找不到“ReCaptcha\RequestMethod\Post"类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆