7

I am updating my website's login system from LightOpenID to Google's Oauth 2.0.

When I require the Client.php and the Service/Oauth2.php I get an error

Fatal error: Class 'Google_Service' not found in /home/myname/repos/website_current/lib/google-api-php-client/src/Google/Service/Oauth2.php on line 32

The code I am using (from my login.php file) looks like this

require_once(dirname($_SERVER['DOCUMENT_ROOT']).'/lib/autoload.php');
require('Google/Client.php');
require('Google/Service/Oauth2.php');
echo "exit";
exit();

I have added the include path in the PHP.ini (in /etc/php5/apache2/php.ini) as

include_path = ".:/usr/local/lib/php:/home/myname/repos/website_current/lib/google-api-php-client/src"

So its seems my Oauth2.php file can't see any of the other includes including the class 'Google_Service' which is one folder up in 'Service.php'.

My folder structure looks like this:

lib/
... autoload.php
... functions.php
... google-api-php-client/
    ... src/
        ... Google/ (etc etc)
public_html/
... login/
    ...login.php

I have no idea why this is occuring. The include path should be seen, and shows up as an included path using phpinfo(); Can someone please give me some insight?

Gambles
  • 157
  • 1
  • 1
  • 10

8 Answers8

18

Make sure you add the line BEFORE any other Google "require_once" lines.

require_once 'google-api-php-client/autoload.php';

I had it last and it had me scratching my head for a good 10 minutes.

Durandal
  • 203
  • 2
  • 7
3

Per the instruction on github:

require_once 'google-api-php-client/autoload.php'; // or wherever autoload.php is located

In your case it seems like the above include url should work fine.

  • I also have required autoload.php. Forgot to add it into the code above (See above edits). Is autoload meant to be in a specfiic place? Maybe the Oauth2.php file cant load classes properly? – Gambles Feb 05 '15 at 19:32
2

The new way of doing this (circa early 2016) is

require_once("Google/autoload.php");

(Assuming you have already set your include path to have /path/to/google-api-php-client/src)

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
  • Not sure if you will see this but I get this error when I add the autoload This library must be installed via composer or by downloading the full package. Do I not already have the full package? – JianYA Mar 05 '18 at 15:09
2

As of Nov 2016

require_once ... 'vendor/autoload.php';
mikeytown2
  • 1,744
  • 24
  • 37
1

To this version https://github.com/google/google-api-php-client this is a posible solution

set_include_path("google-api-php-client/src/" . PATH_SEPARATOR . get_include_path());

//.....

require_once 'Google/Service.php';
//.....
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
1

While working with Google API integration

Fatal error: Class 'abc' not found

error comes when there is definitely something different between the library you have in composer.json above, and the library that is actually being auto-loaded.

had same problem just changed in my composer.json

{"require": {"google/apiclient": "1.0.*@beta"}}

to

{"require": {"google/apiclient": "2.0.*"}}

and then execute php composer.phar update (make sure you give right path for .phar file)

gofr1
  • 15,741
  • 11
  • 42
  • 52
0

Now it is deprecated and moved to Sub Google directory. Following is the new default path: google-api-php-client-master\src\Google\autoload.php

vefthym
  • 7,422
  • 6
  • 32
  • 58
0

After following what Durandal had posted I tried it, but the new path for me is :

require_once 'google-api-php-client/src/Google/autoload.php';

Once I made this changed it worked. Thanks for the help.

thor
  • 21,418
  • 31
  • 87
  • 173
Chris Holcomb
  • 356
  • 2
  • 4