0

I trying to get data from google sheets. but what problem i face is Google_client not found. Php version is updated and i do have client.php in my apiclient/src/google

Fatal error: Uncaught Error: Class 'Google_Client' not found in C:\xampp\htdocs\TestCalon\calonan.php:3 Stack trace: #0 {main} thrown in C:\xampp\htdocs\TestCalon\calonan.php on line 3

<?php
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/client_secret.json');
$client = new Google_Client;
$client->useApplicationDefaultCredentials();

$client->setApplicationName("Something to do with my representatives");
$client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);

if ($client->isAccessTokenExpired()) {
    $client->refreshTokenWithAssertion();
}

$accessToken = $client->fetchAccessTokenWithAssertion()["access_token"];
ServiceRequestFactory::setInstance(
    new DefaultServiceRequest($accessToken)
);

$spreadsheet = (new Google\Spreadsheet\SpreadsheetService)
   ->getSpreadsheetFeed()
   ->getByTitle('Copy of PRU14 Calon');

// Get the first worksheet (tab)
$worksheets = $spreadsheet->getWorksheetFeed()->getEntries();
$worksheet = $worksheets[0];

$listFeed = $worksheet->getListFeed();

/** @var ListEntry */
foreach ($listFeed->getEntries() as $entry) {
   $representative = $entry->getValues();
}

$cellFeed = $worksheet->getCellFeed();

$rows = $cellFeed->toArray();

return $worksheet->getCsv();
joey
  • 73
  • 2
  • 14

2 Answers2

1

Before you call $client = new Google_Client; you should load the Google class (library) file in you code.

That is missing and so resulting as an error.

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
  • How could I load the file? By means of Google/src/Client.php loading and functions like require/require_once I cannot fix anything. – anrequete Oct 01 '18 at 14:44
  • First, you will need to load the libraries which the composer installed to access Google API services. ```require __DIR__ . '/vendor/autoload.php';``` – David Elmasllari Nov 26 '22 at 17:15
0

Please update the composer.json

 "google/apiclient": "^2.12.1"

Inside the composer.json file under the extra update this

 "extra": {
     "google/apiclient-services": [
        "Drive",
        "YouTube"
    ]
}

under the script section at the bottom add this line

"scripts": {
     "pre-autoload-dump": "Google\\Task\\Composer::cleanup"
 }

After adding it, run the command

composer update
composer dump-autoload 

It will work.

CodeNew
  • 39
  • 6