skip to Main Content

Versions:

Composer: 2.4.0

PHP: 7.4.30

Apache: 2.4.29

google-api-php-client: Unknown

Basic Information

On google-api-php-client, I am having trouble creating a client object. I always gives me this error when I try to create one.

Fatal error: Uncaught Error: Class ‘GoogleClient’ not found

It’s starting to bug me quite a bit now. I tried searching online but it currently seems I am the only one with the problem. I followed the instructions here. Up until I got stuck right here, at basic examples. That’s where I use the examples to see if I did everything right with no errors.

I am using composer to load dependencies as well.

Here is my code though.

require_once $coreRoot . 'objects/googleOAuth2/vendor/autoload.php';
$client = new GoogleClient();    
$client->setAuthConfig(/* Secure Key JSON File */);

The $coreRoot variable includes the whole file path that leads up to the directory of the current php file.

What I tried and found

A list of other stack overflow questions I have looked at while trying to search for an answer. None of the questions solved my problem. Some didn’t have any answers, but it has comments.

Class 'Google_Client' not found – Same result

Fatal error: Uncaught Error: Class 'Google_Client' not found – Code not similar to mine.

Fatal error: Uncaught Error: Class 'Google_client' not found despite adding library through composer – No answers but has comments. The comment that helped the asker was confusing. Code most likely didn’t match mine.

Laravel: Class 'Google_Client' not found – No answers but it has comments. Comments was not useful. I also tried the stuff the asker tried and I still got the same result.

Other Information

Composer created Composer.json file

{
    "require": {
        "google/apiclient": "2.12.1"
    }
}

Original Composer.json file

{
    "name": "google/apiclient",
    "type": "library",
    "description": "Client library for Google APIs",
    "keywords": ["google"],
    "homepage": "http://developers.google.com/api-client-library/php",
    "license": "Apache-2.0",
    "require": {
        "php": "^5.6|^7.0|^8.0",
        "google/auth": "^1.10",
        "google/apiclient-services": "~0.200",
        "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0||~6.0",
        "monolog/monolog": "^1.17||^2.0||^3.0",
        "phpseclib/phpseclib": "~2.0||^3.0.2",
        "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0",
        "guzzlehttp/psr7": "^1.8.4||^2.2.1"
    },
    "require-dev": {
        "squizlabs/php_codesniffer": "^3.0",
        "symfony/dom-crawler": "~2.1",
        "symfony/css-selector": "~2.1",
        "cache/filesystem-adapter": "^0.3.2|^1.1",
        "phpcompatibility/php-compatibility": "^9.2",
        "composer/composer": "^1.10.22",
        "yoast/phpunit-polyfills": "^1.0",
        "phpspec/prophecy-phpunit": "^1.1||^2.0",
        "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0"
    },
    "suggest": {
        "cache/filesystem-adapter": "For caching certs and tokens (using Google\Client::setCache)"
    },
    "autoload": {
        "psr-4": {
            "Google\": "src/"
        },
        "files": [
            "src/aliases.php"
        ],
        "classmap": [
            "src/aliases.php"
        ]
    },
    "extra": {
        "branch-alias": {
            "dev-main": "2.x-dev"
        }
    }
}

Notes:

If I forgotten and details or is any part of this was confusing to you, comment below what part was confusing or what I was missing, and I will fix it.

2

Answers


  1. Chosen as BEST ANSWER

    Finally figured out my problem now.

    For my variable $coreRoot, it was set as my localhost aka 127.0.0.1. Which was very wrong. You can't call PHP functions or variables by a web address. That would pose a security vulnerability if it allowed that. You have to use the full file path. For example C:xampphtdocsindex.php, not http://localhost/. Can't believe I missed that.

    Sorry for the trouble to everyone. And to everyone else who has similar problems to, call php from the full address, not the web address.

    I do have to give a little credit to @dılo sürücü answer as it helped me remember this error.


  2. you should use the below code on the current working directory

    ı assume you have an index.php

    require_once 'vendor/autoload.php' ; 
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search