phpcomposer-phprecaptchaautoloadgoogle-cloud-repository

Trouble initializing Google RecaptchaEnterpriseServiceClient


I am trying to add reCAPCHA to my website. Following the README.md from https://github.com/googleapis/google-cloud-php-recaptcha-enterprise, I have used composer to install google/cloud-recaptcha-enterprise on my server:

cd includes
composer require google/cloud-recaptcha-enterprise
cd ..

Then created a simple test.php

<?php

error_reporting(E_ALL);

require 'includes/vendor/autoload.php';

use Google\Cloud\RecaptchaEnterprise\V1\Key;
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;


$client = new RecaptchaEnterpriseServiceClient();

echo "DONE";
?>

But I get a "HTTP ERROR 500" page not working. No warning, no error report. I have looked through the autoloader.php and the paths it generated are correct. I have checked with some other packages I have installed in my includes/ dir and the autoloader works fine with those.

I have tried composer2+PHP8.2 and composer1+PHP7.4.

I have tried not installing to my includes folder.

Trying to debug the autoloader a little, I modified my test.php:

<?php

error_reporting(E_ALL);

$autoloader = require 'includes/vendor/autoload.php';
print_r($autoloader);
echo "</br></br>";
$results = $autoloader->findFile("Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient");
print "Found file for class at: $results</br></br>";

echo $autoloader->loadClass("Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient");

use Google\Cloud\RecaptchaEnterprise\V1\Key;
use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings;
use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;


$client =  new RecaptchaEnterpriseServiceClient();

echo "DONE";
?>

and I get the following output:

Composer\Autoload\ClassLoader Object ( [vendorDir:Composer\Autoload\ClassLoader:private] => /home/<user>/domains/<domain>/public_html/includes/vendor [prefixLengthsPsr4:Composer\Autoload\ClassLoader:private] => Array ( [P] => Array ( [Psr\Http\Message\] => 17 [Psr\Http\Client\] => 16 [Psr\Cache\] => 10 [PHPMailer\PHPMailer\] => 20 ) [G] => Array ( [GuzzleHttp\Psr7\] => 16 [GuzzleHttp\Promise\] => 19 [GuzzleHttp\] => 11 [Grpc\Gcp\] => 9 [Grpc\] => 5 [Google\Type\] => 12 [Google\Rpc\] => 11 [Google\Protobuf\] => 16 [Google\LongRunning\] => 19 [Google\Iam\] => 11 [Google\Cloud\RecaptchaEnterprise\] => 33 [Google\Cloud\] => 13 [Google\Auth\] => 12 [Google\Api\] => 11 [Google\ApiCore\LongRunning\] => 27 [Google\ApiCore\] => 15 [GPBMetadata\Google\Type\] => 24 [GPBMetadata\Google\Rpc\] => 23 [GPBMetadata\Google\Protobuf\] => 28 [GPBMetadata\Google\Longrunning\] => 31 [GPBMetadata\Google\Logging\] => 27 [GPBMetadata\Google\Iam\] => 23 [GPBMetadata\Google\Cloud\Recaptchaenterprise\] => 45 [GPBMetadata\Google\Cloud\] => 25 [GPBMetadata\Google\Api\] => 23 [GPBMetadata\ApiCore\] => 20 ) [F] => Array ( [Firebase\JWT\] => 13 ) ) [prefixDirsPsr4:Composer\Autoload\ClassLoader:private] => Array ( [Psr\Http\Message\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../psr/http-message/src [1] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../psr/http-factory/src ) [Psr\Http\Client\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../psr/http-client/src ) [Psr\Cache\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../psr/cache/src ) [PHPMailer\PHPMailer\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../PHPMailer ) [GuzzleHttp\Psr7\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../guzzlehttp/psr7/src ) [GuzzleHttp\Promise\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../guzzlehttp/promises/src ) [GuzzleHttp\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../guzzlehttp/guzzle/src ) [Grpc\Gcp\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/grpc-gcp/src ) [Grpc\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../grpc/grpc/src/lib ) [Google\Type\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/src/Type ) [Google\Rpc\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/src/Rpc ) [Google\Protobuf\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/protobuf/src/Google/Protobuf ) [Google\LongRunning\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/longrunning/src/LongRunning ) [Google\Iam\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/src/Iam ) [Google\Cloud\RecaptchaEnterprise\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/cloud-recaptcha-enterprise/src ) [Google\Cloud\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/src/Cloud ) [Google\Auth\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/auth/src ) [Google\Api\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/src/Api ) [Google\ApiCore\LongRunning\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/longrunning/src/ApiCore/LongRunning ) [Google\ApiCore\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/gax/src ) [GPBMetadata\Google\Type\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/metadata/Type ) [GPBMetadata\Google\Rpc\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/metadata/Rpc ) [GPBMetadata\Google\Protobuf\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/protobuf/src/GPBMetadata/Google/Protobuf ) [GPBMetadata\Google\Longrunning\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/longrunning/metadata/Longrunning ) [GPBMetadata\Google\Logging\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/metadata/Logging ) [GPBMetadata\Google\Iam\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/metadata/Iam ) [GPBMetadata\Google\Cloud\Recaptchaenterprise\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/cloud-recaptcha-enterprise/metadata ) [GPBMetadata\Google\Cloud\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/metadata/Cloud ) [GPBMetadata\Google\Api\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/common-protos/metadata/Api ) [GPBMetadata\ApiCore\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/gax/metadata/ApiCore ) [Firebase\JWT\] => Array ( [0] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../firebase/php-jwt/src ) ) [fallbackDirsPsr4:Composer\Autoload\ClassLoader:private] => Array ( ) [prefixesPsr0:Composer\Autoload\ClassLoader:private] => Array ( ) [fallbackDirsPsr0:Composer\Autoload\ClassLoader:private] => Array ( ) [useIncludePath:Composer\Autoload\ClassLoader:private] => [classMap:Composer\Autoload\ClassLoader:private] => Array ( [Composer\InstalledVersions] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../composer/InstalledVersions.php [GPBMetadata\GrpcGcp] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/grpc-gcp/src/generated/GPBMetadata/GrpcGcp.php [Grpc\Gcp\AffinityConfig] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/grpc-gcp/src/generated/Grpc/Gcp/AffinityConfig.php [Grpc\Gcp\AffinityConfig_Command] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/grpc-gcp/src/generated/Grpc/Gcp/AffinityConfig_Command.php [Grpc\Gcp\ApiConfig] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/grpc-gcp/src/generated/Grpc/Gcp/ApiConfig.php [Grpc\Gcp\ChannelPoolConfig] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/grpc-gcp/src/generated/Grpc/Gcp/ChannelPoolConfig.php [Grpc\Gcp\MethodConfig] => /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/grpc-gcp/src/generated/Grpc/Gcp/MethodConfig.php ) [classMapAuthoritative:Composer\Autoload\ClassLoader:private] => [missingClasses:Composer\Autoload\ClassLoader:private] => Array ( ) [apcuPrefix:Composer\Autoload\ClassLoader:private] => )

Found file for class at: /home/<user>/domains/<domain>/public_html/includes/vendor/composer/../google/cloud-recaptcha-enterprise/src/V1/RecaptchaEnterpriseServiceClient.php

1

So the autoloader is finding the files, but the namespaces are not correct? The script still does not complete the "DONE" step and the class is not created.

Any help would be appreciated


Solution

  • I'm not sure why my script was not displaying the thrown exception, but debugging a little more via putting echos into the google classes I found this exception:

    Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc
    

    So yeah, creating a credential key file and setting an envirmental variable GOOGLE_APPLICATION_CREDENTIAL solved the current issue.