怪异。知道为什么找不到班级吗?
目录树:
test2.php
- src
- Google
- Spreadsheet
DefaultServiceRequest.php
ServiceRequestInterface.php
Google_Client.php
...
测试2.php:
namespace srcGoogleSpreadsheet;
require_once 'src/Google/Spreadsheet/ServiceRequestInterface.php';
require_once 'src/Google/Spreadsheet/DefaultServiceRequest.php';
require_once 'src/Google/Spreadsheet/Google_Client.php';
use GoogleSpreadsheetServiceRequestInterface;
use GoogleSpreadsheetDefaultServiceRequest;
use GoogleSpreadsheetServiceRequestFactory;
function getGoogleTokenFromKeyFile($clientId, $clientEmail, $pathToP12File) {
require 'src/Google/Spreadsheet/Google_Client.php';
$client = new Google_Client();
$client->setClientId($clientId);
$cred = new Google_Auth_AssertionCredentials(
$clientEmail,
array('https://spreadsheets.google.com/feeds'),
file_get_contents($pathToP12File)
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service_token = json_decode($client->getAccessToken());
return $service_token->access_token;
}
$serviceRequest = new DefaultServiceRequest(getGoogleTokenFromKeyFile(..., ..., ...));
ServiceRequestFactory::setInstance($serviceRequest);
不确定这是否与谷歌API相关还是什么。奇怪的是,在我需要它们之前,ServiceRequest 类将无法工作。当我没有时,它说它找不到它...当我尝试将 src/添加到使用路径时,没有工作,我尝试一起删除路径,但都无所作为。
错误:致命错误:在第 15 行的 test2.php 中找不到类"src\Google\Spreadsheet\Google_Client"
看起来你正在使用这个库
如果使用 Composer 安装库,则需要在代码中包含vendor/autoload.php
文件
require 'vendor/autoload.php';
建议您使用Composer来安装此库,但是如果您不想使用Composer,则需要创建自动加载器并将其require
到代码中