1

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 'Google\Client' 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 Google\Client();    
$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
    1) Why are you not using composer to load dependencies? 2) Why are you using PHP 7.2? – John Hanley Aug 18 '22 at 02:40
  • I am using composer to load dependencies. Sorry about that, guess I forgot to mentioned that part. Why I am using PHP 7.2, I was unable to update it. I tried updating it before but apache didn't like that and refused to start. – Jonathan J. Pecany Aug 18 '22 at 02:59
  • 1
    Include composer.json in your post. Which version of Apache? I have no problem using PHP 7.4, 8.0, and 8.1 with Apache. You might have more than one problem. – John Hanley Aug 18 '22 at 03:05
  • Okay, added those information in now. I probably do have more than one problem, although other composer stuff has worked previously and I didn't change anything since I last used composer. – Jonathan J. Pecany Aug 18 '22 at 03:38
  • You have not set up composer correctly. Start over and use composer to create your project and add dependencies. – John Hanley Aug 18 '22 at 03:40
  • Actually, I have two composer.json files. One in the downloaded one I got from github and another one that was created from composer. I gave you the one that was created from composer. – Jonathan J. Pecany Aug 18 '22 at 03:55
  • 2
    Please check whether current versions of the tools help you out. PHP 7.2 is horribly outdated, Composer 1.9.1 is nearly three years old, and having a unknown version of any package in your `composer.json` also looks pretty strange to me – Nico Haase Aug 18 '22 at 06:56
  • Also, why do you use `require_once $coreRoot . 'objects/googleOAuth2/vendor/autoload.php';` and not the proper autoloader that is given in the root vendor folder of your application? – Nico Haase Aug 18 '22 at 06:56
  • @NicoHaase I cannot update PHP. Apache will not let me, no matter how much I tried took. Just spent the last couple of hours trying to update it and still nothing. I did update my composer however though. – Jonathan J. Pecany Aug 18 '22 at 07:03
  • 1
    And did you try to use the proper autoloader that is generated by Composer? – Nico Haase Aug 18 '22 at 07:18
  • I asked on reddit, but I do doubt anyone could help though. – Jonathan J. Pecany Aug 18 '22 at 16:39
  • @JohnHanley Until I managed to update PHP, got any others ideas on the composer problem? – Jonathan J. Pecany Aug 18 '22 at 21:56
  • run `composer diagnose` and run it to show the platform packages (`composer show -p`). [edit] your question with the current information, remove the outdated infos. – hakre Aug 18 '22 at 22:28
  • 1
    Basically, you want to run `composer init` followed by `composer require ...`. The correct vendor path should be `require 'vendor/autoload.php';` Google search. There are many tutorials on how to use composer correctly. – John Hanley Aug 18 '22 at 22:31
  • recommended is to run both as well with `-n` (non-interactive) so that the setup is already self-documented. you don't find that in the tutorials. – hakre Aug 18 '22 at 22:35
  • You guys do know that I am just trying to install google-api-php-client, right? I also finally updated my PHP to 7.4 now. Still gotten the same error though. – Jonathan J. Pecany Aug 19 '22 at 03:41
  • I managed to fix it. It is very embarrassing. I had my file location set to my localhost, not my full file directory. – Jonathan J. Pecany Aug 19 '22 at 04:06
  • Sorry, it completely slipped my mind. Again, truly sorry for wasting your time. I did put my solution in my answers to help others who may make the same mistake like me. – Jonathan J. Pecany Aug 19 '22 at 04:15

2 Answers2

1

you should use the below code on the current working directory

ı assume you have an index.php

require_once 'vendor/autoload.php' ; 

dılo sürücü
  • 3,821
  • 1
  • 26
  • 28
0

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:\xampp\htdocs\index.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.