2012-08-01 55 views
3

我試着在我的代碼使用此功能:https://developers.google.com/drive/v2/reference/files/list谷歌驅動PHP客戶端庫檢索文件的資源列表

這裏如下:

/** 
* Retrieve a list of File resources. 
* 
* @param apiDriveService $service Drive API service instance. 
* @return Array List of File resources. 
*/ 
function retrieveAllFiles($service) { 
    $result = array(); 
    $pageToken = NULL; 

    do { 
    try { 
     $parameters = array(); 
     if ($pageToken) { 
     $parameters['pageToken'] = $pageToken; 
     } 
     $files = $service->files->listFiles($parameters); 

     array_merge($result, $files->getItems()); // <---- Exception is throw there ! 
     $pageToken = $files->getNextPageToken(); 
    } catch (Exception $e) { 
     print "An error occurred: " . $e->getMessage(); 
     $pageToken = NULL; 
    } 
    } while ($pageToken); 
    return $result; 
} 

但我得到這個錯誤:

Fatal error: Call to a member function getItems() on a non-object in C:\Program Files (x86)\EasyPHP-5.3.6.1\www\workspace\CPS\class\controller\CtrlGoogleDrive.php on line 115

你陣列看起來可能是空的但它不應該是:

Array 
(
    [kind] => drive#fileList 
    [etag] => "WtRjAPZWbDA7_fkFjc5ojsEvE7I/lmSsH-kN3I4LpwShGKUKAM7cxbI" 
    [selfLink] => https://www.googleapis.com/drive/v2/files 
    [items] => Array 
     (
     ) 

) 

回答

8

PHP客戶端庫可以以兩種方式運行,並返回對象或關聯數組,後者是默認值。

文檔中的例子假設你希望庫返回對象,否則,你將不得不替換以下兩個調用:

$files->getItems() 
$files->getNextPageToken() 

與使用關聯數組來代替相應的調用:

$files['items'] 
$files['nextPageToken'] 

更妙的是,你可以配置磁帶庫始終通過設置

$apiConfig['use_objects'] = true; 
返回對象

請檢查config.php文件的詳細配置選項:

http://code.google.com/p/google-api-php-client/source/browse/trunk/src/config.php

+0

感謝克勞迪奧應該是吧,我真的很感激你給每個我對谷歌雲端硬盤API問題的幫助;) – 2012-08-01 18:32:08

+0

而且這加工!再次感謝 ;) – 2012-08-01 18:37:18

相關問題