我一直在尝试让 google api php 客户端在应用引擎上工作。在阅读了文档提供的 app.yaml 配置资源后,我仍然无法让它工作。我的 app.yaml 有 google-api-php-client 如下:
- url: /google-api-php-client/(.*?)/(.*?)/(.*)
script: google-api-php-client/\3/\2/\1.php
并且,文件夹的结构是:
google-api-php-client->(some_folders)->(some_folders+files)->(some_files)
| level1 | | level2 | | level3 | | level4 |
我想设置配置,以便我能够访问级别 3 和级别 4 中的文件,并且能够像这样进行 require_once 调用:
require_once ('/google-api-php-client/src/Google_Client.php');
require_once ('/google-api-php-client/src/contrib/Google_DriveService.php');
我可以在 localhost 上执行这些 require_once 调用,但在将文件部署到应用引擎时却不行。应用引擎仪表板上的日志显示:
PHP Fatal error: require_once(): Failed opening required '/google-api-php-client/src/Google_Client.php' (include_path='.;/base/data/home/apps/s~...
我将不胜感激任何和所有输入。谢谢!
编辑:我在使用 require_once 的地方添加代码
require_once ('google-api-php-client/src/Google_Client.php');
require_once ('google-api-php-client/src/contrib/Google_DriveService.php');
session_start();
$client = new Google_Client();
$client->setApplicationName("Drive Demo");
$client->setClientId('****');
$client->setClientSecret('****');
$client->setRedirectUri('http://localhost:8080');
$client->setDeveloperKey('****');
$client->setScopes(array(
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'));
$client->setAccessType('offline');
$service = new Google_DriveService($client);
$fileId = '****';
function printFile($service, $fileId) {
try {
$file = $service->files->get($fileId);
print_r($file);
print "Title: " . $file->getTitle();
print "Description: " . $file->getDescription();
print "MIME type: " . $file->getMimeType();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array('q'=>'', 'maxResults'=> '25', 'fields'=>'items(title,description,mimeType)');
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
if (!isset($_REQUEST['code']) && !isset($_SESSION['access_token'])) {
$authUrl = $client->createAuthUrl();
print("<a href='".$authUrl."'>Authorize me</a>");
}else {
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
$_SESSION['access_token'] = $client->getAccessToken();
}
$files = retrieveAllFiles($service);
foreach($files as $file){
//print_r($file);
print "Title: " . $file->getTitle().'<br/>';
print "Description: " . $file->getDescription().'<br/>';
print "MIME type: " . $file->getMimeType().'<br/>';
}
}
更新:根据@Stuart 的评论删除了“/”,从而解决了这个问题。