【问题标题】:Folder Structure for App Engine using PHP to Set Up the Drive API使用 PHP 设置 Drive API 的 App Engine 的文件夹结构
【发布时间】:2015-04-01 00:43:09
【问题描述】:

用于设置 Google Drive API 以使用 PHP 的 Google 文档示例具有这样的要求语句:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

other code
?>

我从 GitHub 下载了一个 .zip 文件,并将其解压缩。在提取的文件夹和文件中,来自 GitHub 的 google-api-php-client 文件包含名为 Google_Client.php 的文件。在 src/Google/ 目录中有一个名为 Client.php 的文件。

而且也没有 contrib 文件夹。

AND,GitHub google-api的下载中没有名为DriveService.php的文件-php-client 文件。

所以,我假设对文件和文件夹的引用应该如下所示:

<?php
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Drive.php';

other code
?>

Client.php 文件包含 require_once 语句,用于 autoload.php 文件:

if (!class_exists('Google_Client')) {
  require_once dirname(__FILE__) . '/../autoload.php';
}

引用/../ 中的双点表示当正在搜索文件autoload.php 以便包含时,转到父文件夹,并在那里搜索。

所以文件夹结构应该是:

src - Folder
  autoload.php

  Google - Folder  
    Client.php
    DriveService.php

autoload.php文件代码为:

<?php

function google_api_php_client_autoload($className)
{
  $classPath = explode('_', $className);
  if ($classPath[0] != 'Google') {
    return;
  }
  // Drop 'Google', and maximum class file path depth in this project is 3.
  $classPath = array_slice($classPath, 1, 2);

  $filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php';
  if (file_exists($filePath)) {
    require_once($filePath);
  }
}

spl_autoload_register('google_api_php_client_autoload');

Drive.php 文件中没有 requireinclude 语句。

下载的文件中还有一个Service文件夹,Google文件夹中有一个Service.php文件。 p>

文档是否错误,我的建议是否正确? PHP 中 Drive API 的正确设置是什么?

【问题讨论】:

    标签: php google-app-engine google-drive-api require


    【解决方案1】:

    来自 GitHub 的 zip 文件的文件夹结构与从 Subversion 获得的文件和文件夹的文件夹结构不同。使用 Subversion 下载 Google API PHP Client Library 时,有一个 contrib 文件夹,其中有名为 Google_Client.php 的文件Google_DriveService.php。我已经从 GitHub 下载并从 zip 文件中提取了文件。这会产生非常不同的结果,然后使用 Subversion 下载文件。 Subversion 会重命名我猜想的文件,并创建不同的文件夹名称。因此,随着使用 Subversion 新下载文件夹和文件,文件夹结构和文件名与文档中的示例一致。

    来自一些旧文档中的入门页面:

    https://code.google.com/p/google-api-php-client/wiki/GettingStarted

    我找到了一些信息:

    提取库后。 . . . . ,您将拥有一个新的 google-api-php-client-x.y 目录。库文件本身位于 google-api-php-client/src 目录中

    所以,你需要得到一个:

    google-api-php-client
    

    目录和一个:

    google-api-php-client/src
    

    目录。

    引用:

    获得图书馆后。 . ,您将在文件系统的某处有一个名为 google-api-php-client 的目录,其中包含库文件。具体来说,您需要在脚本中包含文件 src/Google_Client.php 和 src/contrib/Google_PlusService.php。

    以上引用适用于 Google_PlusService.php,但可以替换为您需要使用的任何 API 服务文件。

    有多种方法可以将所需的库文件包含到您的代码中。

    文件和文件夹的命名约定似乎发生了变化,迁移到新库版本的文档中对此进行了说明。

    https://developers.google.com/api-client-library/php/guide/migration

    在使用 Drive API 之前,需要进行授权。我查看的初始文档是针对命令行 应用程序的。我不想要命令行应用程序,我正在尝试构建一个 Web 应用程序。所以,需要先实现服务端授权。

    https://developers.google.com/drive/web/auth/web-server

    服务器端授权需要在您的代码中包含一个额外的 php 文件。

    require_once "google-api-php-client/src/Google/Service/Oauth2.php";
    

    文档在这里:

    https://developers.google.com/drive/web/auth/web-server

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多