2016-03-28 173 views
0

我正在使用google api客户端上传文件到驱动器,运行google developer site中列出的快速启动示例列出可正常工作的文件。无法重新声明类Google_Service_Drive

为了在特定的父文件夹中插入文件,我使用了两个导致如下所示错误的对象(请参见下面两行)。

线的造成错误(第二行导致错误):

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

错误:

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 

全码:(只显示最后两行由我添加的,其余的是从谷歌开发人员网站)

<?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(); 

?> 

请帮忙。

回答

0

avil,我猜你遇到了同样的问题,我遇到了。我跟着这个页面上的“快速开始”的指令:

https://developers.google.com/drive/v3/web/quickstart/php#prerequisites

安装appclient:1. *使用作曲家,然后它说,:

“下载并解压缩该zip文件,并复制硬盘.php到vendor/google/apiclient/src/Google/Service /,替换现有文件。“

我得到了同样的错误,并起了疑心,所以我检查了GIT看着这个文件的历史记录:

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

你会发现该驱动器的“V1主”的版本。 php比2015年10月的发行版差不多2000行代码。

从最新版本中缺少您的“Google_Service_Drive_ParentReference”(我正在寻找Google_Service_Drive_Property类)的所有代码。

我从GIT repo的历史记录(2015年10月9日)中将代码复制到我的Drive.php文件中,现在工作正常。

希望这会有所帮助,

+0

嗨,非常感谢您的帮助。我下载了旧版本的Drive.php,并且必须更改几个函数(getFiles - > getItems和setName - > setTitle等),但最后它运行良好。 – avil