2016-03-28 13 views
0

私はドライブにファイルをアップロードするためにgoogle apiクライアントを使用しています。google developer siteに記載されているクイックスタートの例を実行して正常に動作しているファイルを一覧表示します。クラスGoogle_Service_Driveを再宣言できません

特定の親フォルダにファイルを挿入するために、私は以下の2つのオブジェクトを使用しています(下の2行を参照)。 (第2の行は、エラーの原因となっている)エラーの原因となっている

回線:

$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 

フルコード:(唯一の最後の2行は、私が追加され、残りはです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(); 

?> 

助けてください。

答えて

0

avil、私はあなたと同じ問題に遭遇したと思います。ドライブを

「をダウンロードし、このzipファイルを解凍してコピーします。1. *作曲を使用して、それはその後、言うまで:

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

はでappclientをインストールするには:私は、このページの指示に従って、「クイックスタート」に続きます.phpをベンダ/ 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月の以前のリリースよりも約2,000行短いコードです。最新のリリースでは、Google_Service_Drive_ParentReference(Google_Service_Drive_Propertyクラスを探していました)のコードがすべてありません。

私はGITリポジトリの履歴(2015年10月9日)のコードをDrive.phpファイルにそのままコピーしました。現在は正常に動作しています。

希望します。

+0

こんにちは、ありがとうございました。私はDrive.phpの古いバージョンをダウンロードし、(getFiles - > getItemsとsetName - > setTitleなど)のようないくつかの関数を変更しなければならなかったが、それは最後にはうまくいった。 – avil

関連する問題