【问题标题】:How to configure app.yaml to load google-api-php-client on app engine?如何配置 app.yaml 以在应用引擎上加载 google-api-php-client?
【发布时间】:2013-10-22 17:17:46
【问题描述】:

我一直在尝试让 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 的评论删除了“/”,从而解决了问题。

【问题讨论】:

    标签: php google-app-engine


    【解决方案1】:

    在这种情况下,您不需要配置 app.yaml - 因为您不想将任何请求直接路由到这些脚本。

    只需将 google-api-php-client 的源代码放在与您的应用相同的目录中,然后使用 appcfg.py 进行部署即可。

    查看我关于配置客户端here的文章。

    【讨论】:

    • 感谢@stuart 的回复。我按照博客中的说明进行操作,但仍然收到:PHP 致命错误:require_once(): Failed opening required '/google-api-php-client/src/Google_Client.php' (include_path='.;/base/data /home/apps/s~...你还有什么想法吗?
    • 是的,不要在路径的开头添加'/' - require_once 'google-api-php-client/src/Google_Client.php';
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    相关资源
    最近更新 更多