Google ReCaptcha 2 Fatal error: Class 'ReCaptcha\RequestMethod\Post' not found - php

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?

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.
Place a copy of it in your recaptcha top level folder (the one with README.md in it)
Replace line 12 with $prefix = 'ReCaptcha\\';
replace line 15 with $base_dir = __DIR__ . '/src/ReCaptcha/';
Require autoloader.php (this file) in your code somewhere
Instantiate your ReCaptcha object something like this in your code
new ReCaptcha\ReCaptcha($RECAPTCHASECRETKEY);

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

Related

Google GCP error with tutorial code

Resolving the issue. My Case:
Before reading the question, my issue was solved due to my development environment. Using CodeKit (an application on MacOS), upon building my code from the source folder, items such as the composer.json and other files did not transfer causing the issues described below. If this does happen to you scout the two folder to look for discrepancies the paste the missing docs from the src to the build folder.
:: QUESTION ::
I am starting to use GCP today and after following the instructions defined here:
composer require google/cloud-storage
then:
putenv("GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json");
require __DIR__.'/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$myid = "my-project-id";
$storage = new StorageClient([
'projectId'=>$myid
]);
var_dump($storage->buckets());
When running this i get the following error:
Fatal error: Class 'Google\Auth\Cache\MemoryCacheItemPool' not found in /place/to/vendor/google/cloud-core/RequestWrapperTrait.php on line 94
I have no idea how to solve the issue, as i am just getting started with GCP. No idea whether this is a problem with the platform or my code.
File structure appears as follows for the Google Auth:
vendor
google
auth
src
tests
cloud-core
cloud-storage
the /Cache/MemoryCacheItemPool exists inside both the tests and src folder, but the above is referencing it minus the src or tests folder.
I have also ran:
composer update
and uninstalled and reinstalled the package to no effect
Google Cloud Project Link
Where did you find the code that you used and pasted? Because the one present in the official documentation is different.
This portion of code is the one in the tutorial you linked, try to use the client library and post the error logs if get any!
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$projectId = 'YOUR_PROJECT_ID';
$storage = new StorageClient(['projectId' => $projectId]);
$bucketName = 'my-new-bucket';
$bucket = $storage->createBucket($bucketName);
echo 'Bucket ' . $bucket->name() . ' created.';
Remember that bucket name should be unique and therefore I would advice you to test it with a long complex name to avoid to hit already used names, and always test the result of the operation.
UPDATE
I tested also your code and it is working as well, therefore I believe that is an error in the setup of the environment.
Did you get any error while running the composer require google/cloud-storage? Because the class that is missing Google\Auth\Cache\MemoryCacheItemPool is part of Psr that is installed by the composer
[...]
Installing psr/cache (1.0.1)
Loading from cache
[...]
UPDATE2
Matthew M found the error in its configuration and posted:
Finally resolved the issue. I'm using CodeKit in my working
environment and it looks like it is changing something when it
compiles. Ran an uncompiled version and it's working fine.

authorization of app using pinterest api php and create board

i am using dirkgroenen library php for authorization of my app and create board
which i downloaded from here
https://github.com/dirkgroenen/Pinterest-API-PHP
evry time i run this code it gives me uncaught error about the line:8
Fatal error: Uncaught Error: Class 'Dotenv\Dotenv' not found in C:\xampp\htdocs\pin\vendor\demo\boot.php:8
here is the code for boot.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require ("../autoload.php");
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
$pinterest = new
DirkGroenen\Pinterest\Pinterest(getenv("4915445646307112766"),
getenv("1ae8683b277958bd9739cedb0f0b1434eee42176dd3d44cbf44981f9acaadd94"));
if (isset($_GET["code"]))
{
$token = $pinterest->auth->getOAuthToken($_GET["code"]);
$pinterest->auth->setOAuthToken($token->access_token);
setcookie("access_token", $token->access_token);
}
else if (isset($_GET["access_token"]))
{
$pinterest->auth->setOAuthToken($_GET["access_token"]);
}
else if (isset($_COOKIE["access_token"]))
{
$pinterest->auth->setOAuthToken($_COOKIE["access_token"]);
}
else
{
assert(false);
}
?>
can anyone please help me to fix it.. i have spent more than 48 hours to resolve this issue..
any help will be greatly appreciated
Obviously, you don't have Dotenv class anywhere where PHP expects it to be found.
Options:
You don't have it at all, then download it from here: vlucas/phpdotenv. Composer should have downloaded it, BUT only if you used composer to install DirkGroenen.
You have it but it is outside autoload path - check autoload path, make sure that Dotenv is located in appropriate vendor-folder.
You have it but it doesn't support autoload (sic!) - check with latest version OR include it directly with include/require
It requires phpunit/phpunit to be installed aswell.

Composer (Facebook Ads SDK) setup with CodeIgniter

I have a CodeIgniter application. I've been trying to set it up to work with Facebook ADS SDK ( https://github.com/facebook/facebook-php-ads-sdk ).
I want to use a simple lines in my controller like:
use FacebookAds\Object\CustomAudience;
use FacebookAds\Object\Fields\CustomAudienceFields;
use FacebookAds\Object\Values\CustomAudienceSubtypes;
$audience = new CustomAudience(null, 'act_123123');
I have created composer.json file:
{
"require": {
"facebook/php-ads-sdk": "2.8.*"
}
}
I let it through command
php composer.phar install --no-dev
And everything worked fine. It installed me autoload.php with external facebook folder.
Now when it comes to the part where I need to make it work with CodeIgniter I constantly get errors.
I tried two different approaches:
First was to include it in my index.php Like this:
include_once 'application/vendor/autoload.php';
require_once BASEPATH.'core/CodeIgniter.php';
However I was getting errors for not loading my classes that are in application/core folder (Back_Controller is extending CI_Controller and I am using Back_Controller in my every controller, I also have My_Model and Front_Controller there).
Second aproach:
To use CodeIgniter's feature to use Auto-loader together with Composer. So I changed in the config:
$config['composer_autoload'] = TRUE;
Both approaches returned me the same errors:
"Fatal error: Class 'Back_Controller' not found in /public_html/application/controllers/admin/Shops.php on line 4
Warning: include(): open_basedir restriction in effect.
File(application/errors/html/error_php.php) is not within the allowed
path(s): () in /public_html/system/core/Exceptions.php on line 269"
How can I set it up so Facebook SDK actually works together with CodeIgniter?
Any help will be very appreciated. Thank you.
I am thinking that one of the solutions would be to add "autoload" to composer.json so it loads my core classes but I can't figure out how should it be. I tried:
"autoload": {
"psr-4": {
"Back_Controller\\":"core/"
}
}
Try the following:
Include the main Api file
include APPPATH.'vendor/wherever_your_sdk_is/FacebookAds/Api.php';
Then do the following... as per documentation...
use FacebookAds\Api;
// Initialize a new Session and instanciate an Api object
Api::init($app_id, $app_secret, $access_token);
// The Api object is now available trough singleton
$api = Api::instance();
Figured it out.
I just added "autoload" to composer.json to autoload my core files and now both solutions that I tried before - works.
"autoload": {
"psr-4": {
"":"core/"
}
}

How to include or bootstrap the paypal-core-sdk php

I've downloaded the paypal-core-sdk for php from GitHub manually, my server does not have 'composer' and I've just manually copied the files to an include directory outside the document root. Problem is when I go call on some of the classes the server borks and doesn't know what I'm talking about ... i.e.
Fatal error: Class 'PPApiContext' not found in /home/rctoronto/public_html/start.php on line 143
if I include once on the file providing PPApiContext, it moves along to another error and so on eventually proceeds when I've included all the ones it complains about. But then the sdk has internal dependency issues.
How do I formally declare the sdk library so my app can use it - I've scoured the web and haven't managed to find a good answer to this question. I've tried manually altering my include_path using my php.ini and still it can't use the classes of the sdk. I don't have access to /usr/lib/php or /usr/local/lib/php as I am on a shared server.
this is the 'getAuthorize' url for paypal (which I've gotten to work with manually include_once on the files...
$apicontext = new PPApiContext(array('mode' => 'live'));
$clientId = PP_CLIENTID;
$clientSecret = PP_SECRET;
$scope = array('openid', 'email');
$redirectUri = 'https://maskedurl.ca/login?ltype=pp&loginSubmit=true';
$pp_authUrl = PPOpenIdSession::getAuthorizationUrl($redirectUri, $scope , $clientId, $apicontext);
But how do we properly declare the sdk library in whole?
From: http://paypal.github.io/sdk/
If you do not want to use composer, you can grab the SDK zip, unzip the file to your application folder and require the 'vendor/autoload.php' file. This file registers a custom autoloader that can autload the PayPal SDK files.
Download SDK zip
Here's how I solved it. Update composer.json as follows:
{
"name": "PayPal Test",
"require": {
"paypal/sdk-core-php": "dev-stable"
},
"autoload": {
"psr-0": {"PayPal\\": "vendor/paypal/sdk-core-php/lib/"}
}
}
This will do the job:
include('paypal-sdk-core/lib/common/PPApiContext.php');
include('paypal-sdk-core/lib/PPConstants.php');
include('paypal-sdk-core/lib/PPConfigManager.php');
include('paypal-sdk-core/lib/auth/openid/PPOpenIdSession.php');
If you need the full stack to make the example on https://devtools-paypal.com/guide/openid/php?interactive=ON&env=sandbox work, use this:
include('paypal-sdk-core/lib/common/PPApiContext.php');
include('paypal-sdk-core/lib/PPHttpConfig.php');
include('paypal-sdk-core/lib/common/PPUserAgent.php');
include('paypal-sdk-core/lib/PPHttpConnection.php');
include('paypal-sdk-core/lib/PPLoggingLevel.php');
include('paypal-sdk-core/lib/exceptions/PPConnectionException.php');
include('paypal-sdk-core/lib/common/PPModel.php');
include('paypal-sdk-core/lib/auth/IPPThirdPartyAuthorization.php');
include('paypal-sdk-core/lib/handlers/IPPHandler.php');
include('paypal-sdk-core/lib/PPConstants.php');
include('paypal-sdk-core/lib/PPConfigManager.php');
include('paypal-sdk-core/lib/auth/openid/PPOpenIdSession.php');
include('paypal-sdk-core/lib/auth/PPTokenAuthorization.php');
include('paypal-sdk-core/lib/auth/openid/PPOpenIdTokeninfo.php');
include('paypal-sdk-core/lib/transport/PPRestCall.php');
include('paypal-sdk-core/lib/PPLoggingManager.php');
include('paypal-sdk-core/lib/handlers/PPOpenIdHandler.php');

Symfony Class Not Found

I am getting the error:
PHP Fatal error: Class 'Symfony\\Component\\Process\\PhpProcess' not found in ...
It's my first time using Symfony. I download the files from the GitHub site. Since the downloaded files are of the Process directory, I created folders in order to have the correct path. (Symfony/Component/Process/DOWNLOADED FILES).
I placed the Symfony directory in the main directory (where my index file is).
Is this how you install Symfony? (Note I only need the Process sub-directory and not the whole framework)
For now I am using this ready-made code, just to see if I can get it running:
use Symfony\Component\Process\PhpProcess;
$command = file_get_contents('/hello_world.php');
if ( $command ) {
$process = new PhpProcess($command); //<----- GETTING ERROR HERE
$process->run();
if ($process->isSuccessful()) {
$output = $process->getOutput();
// ...
}
else {
throw new Exception($process->getErrorOutput());
}
}
Why am I getting this error and how can I fix it please? Is it due to a bad path?
I think it has to do with namespace registration, have you checked that the namespace that defines the class is loaded?, check the ClassLoader reference
Check the autoload.php file inside your app directory

Categories