简体   繁体   English

无法重新声明类Google_Service_Drive

[英]Cannot redeclare class Google_Service_Drive

I am using google api client for uploading a file to drive, ran the quickstart example given in google developer site to list the files which is working fine. 我正在使用google api客户端上传文件进行驱动,运行了google developer网站中给出的快速入门示例以列出运行良好的文件。

In order to insert a file in specific parent folder I am using two objects (see the two lines below) which are causing an error shown below. 为了在特定的父文件夹中插入文件,我正在使用两个对象(请参见下面的两行),这将导致以下所示的错误。

Lines that are causing error (second line is causing the error): 导致错误的行(第二行导致错误):

$file = new Google_Service_Drive_DriveFile();
$parent = new Google_Service_Drive_ParentReference(); 

Error: 错误:

PHP Fatal error:  Cannot redeclare class Google_Service_Drive in C:\xampp\htdocs\driveapi\vendor\google\apiclient\src\Google\Service\Drive.php on line 729
PHP Stack trace:
PHP   1. {main}() C:\xampp\htdocs\driveapi\drive_client.php:0
PHP   2. spl_autoload_call() C:\xampp\htdocs\driveapi\drive_client.php:92
PHP   3. Composer\Autoload\ClassLoader->loadClass() C:\xampp\htdocs\driveapi\drive_client.php:92
PHP   4. Composer\Autoload\includeFile() C:\xampp\htdocs\driveapi\vendor\composer\ClassLoader.php:301

Full code: (only last two lines are added by me, rest is from google developer site) 完整代码:(我仅添加了最后两行,其余的来自Google开发者网站)

<?php require __dir__ . '/vendor/autoload.php';

define('APPLICATION_NAME', 'drivephp');
define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json');
define('CLIENT_SECRET_PATH', __dir__ . '/client_secret.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-php-quickstart.json
define('SCOPES', implode(' ', array("https://www.googleapis.com/auth/drive")));

if (php_sapi_name() != 'cli') {
    throw new Exception('This application must be run on the command line.');
}

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfigFile(CLIENT_SECRET_PATH);
    $client->setAccessType('offline');

    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    if (file_exists($credentialsPath)) {
        $accessToken = file_get_contents($credentialsPath);
    } else {
        // Request authorization from the user.
        $authUrl = $client->createAuthUrl();
        printf("Open the following link in your browser:\n%s\n", $authUrl);
        print 'Enter verification code: ';
        $authCode = trim(fgets(STDIN));

        // Exchange authorization code for an access token.
        $accessToken = $client->authenticate($authCode);

        // Store the credentials to disk.
        if (!file_exists(dirname($credentialsPath))) {
            mkdir(dirname($credentialsPath), 0700, true);
        }
        file_put_contents($credentialsPath, $accessToken);
        printf("Credentials saved to %s\n", $credentialsPath);
    }
    $client->setAccessToken($accessToken);

    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
        $client->refreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, $client->getAccessToken());
    }
    return $client;
}

/**
 * Expands the home directory alias '~' to the full path.
 * @param string $path the path to expand.
 * @return string the expanded path.
 */
function expandHomeDirectory($path) {
    $homeDirectory = getenv('HOME');
    if (empty($homeDirectory)) {
        $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
    }
    return str_replace('~', realpath($homeDirectory), $path);
}

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
$parentId = null;

// Print the names and IDs for up to 10 files.
$optParams = array(
//      'pageSize' => 10, 'fields' =>
//      "nextPageToken, files(id, name)"
);
$results = $service->files->listFiles($optParams);

if (count($results->getFiles()) == 0) {
    print "No files found.\n";
} else {
    print "Files:\n";
    foreach ($results->getFiles() as $file) {
        printf("%s (%s)\n", $file->getName(), $file->getId());
    }
}

$file = new Google_Service_Drive_DriveFile();
$parent = new Google_Service_Drive_ParentReference();

?>

Please help. 请帮忙。

avil, I'm guessing you ran into the same issue that I ran into. 砧,我猜你遇到了我遇到的同样的问题。 I followed the "quickstart" instructions on this page: 我按照此页面上的“快速入门”说明进行操作:

https://developers.google.com/drive/v3/web/quickstart/php#prerequisites https://developers.google.com/drive/v3/web/quickstart/php#前提条件

to install the appclient:1.* using composer and it then says to: 使用composer安装appclient:1. *,然后说:

"Download and extract this zip file and copy Drive.php to vendor/google/apiclient/src/Google/Service/, replacing the existing file." “下载并解压缩此zip文件,然后将Drive.php复制到vendor / google / apiclient / src / Google / Service /中,替换现有文件。”

I got the same error and became suspicious so I checked on GIT and looked at the history of this file: 我遇到了同样的错误并变得可疑,因此我检查了GIT,并查看了该文件的历史记录:

https://github.com/google/google-api-php-client/blob/v1-master/src/Google/Service/Drive.php https://github.com/google/google-api-php-client/blob/v1-master/src/Google/Service/Drive.php

You'll notice that the "v1-master" version of Drive.php is almost 2,000 lines of code shorter than the prior release in October 2015. 您会注意到,Drive.php的“ v1-master”版本比2015年10月的先前版本短了近2,000行代码。

All of the code for your "Google_Service_Drive_ParentReference" (I was looking for the Google_Service_Drive_Property class) is missing from the newest release. 最新版本中缺少“ Google_Service_Drive_ParentReference”的所有代码(我一直在寻找Google_Service_Drive_Property类)。

I literally copied the code from the GIT repo's History (Oct 9 2015) into my Drive.php file and it's working fine now. 我将代码从GIT存储库的历史记录(2015年10月9日)中复制了到我的Drive.php文件中,现在可以正常工作了。

Hope this helps, 希望这可以帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM