【问题标题】:Fatal error: Uncaught Error: Class 'Google_Client' not found致命错误:未捕获的错误:找不到类“Google_Client”
【发布时间】:2018-04-30 06:34:59
【问题描述】:

我试图从谷歌表格中获取数据。但我面临的问题是找不到 Google_client。 PHP 版本已更新,我的 apiclient/src/google 中有 client.php

致命错误:未捕获的错误:在 C:\xampp\htdocs\TestCalon\calonan.php:3 中找不到类 'Google_Client' 堆栈跟踪:#0 {main} 在 C:\xampp\htdocs\TestCalon\calonan 中抛出.php 在第 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();

【问题讨论】:

  • 在调用 $client = new Google_Client; 之前,您应该在代码中加载 Google 类(库)文件。
  • 得到它。非常感谢。
  • 酷。将其发布为答案。
  • @HimanshuUpadhyay 我如何加载 Google 类库。

标签: php


【解决方案1】:

调用之前 $client = new Google_Client;您应该在代码中加载 Google 类(库)文件。

这是缺失的,因此导致错误。

【讨论】:

  • 如何加载文件?通过 Google/src/Client.php 加载和 require/require_once 之类的功能,我无法修复任何问题。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2018-09-06
  • 2018-05-11
  • 2020-11-09
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多