2013-11-14 20 views
0

我試圖使用PHP訪問和更新Google Drive中的文件。一切都很好,直到我嘗試調用$ file_to_update-> setTitle(「NEW TITLE」)。Google Drive API - 嘗試更新文件時調用非對象的成員函數

我可以下載文件的元數據,但我無法更新任何內容。

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



$client = new Google_Client(); 
// Get your credentials from the console 
$client->setClientId(''); 
$client->setClientSecret(''); 
$client->setRedirectUri(''); 
$client->setScopes(array('')); 

$service = new Google_DriveService($client); 

$authUrl = $client->createAuthUrl(); 

//Request authorization 
print "Please visit:\n$authUrl\n\n"; 
print "Please enter the auth code:\n"; 
$authCode = trim(fgets(STDIN)); 

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


retrieveAllFiles($service); 

function retrieveAllFiles($service) { 
$result = array(); 
$pageToken = NULL; 

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


    $fileIDs = array(); 

    $file = ($files[items]); 
    foreach($file as $f){ 
    array_push($fileIDs, $f["id"]); 
    print $f["id"]."\n"; 
    } 

    $str = $fileIDs[1]; 

    $file_to_update = $service->files->get($str); 

    $file_to_update->setTitle("NEW TITLE"); 

} catch (Exception $e) { 
    print "An error occurred: " . $e->getMessage(); 
    $pageToken = NULL; 
} 
} while ($pageToken); 
return $result; 
} 

回答

1

您對$ service-> files-> get($ str)的調用不返回對象。

如果你檢查在功能:

public function get($fileId, $optParams = array()) { 
    $params = array('fileId' => $fileId); 
    $params = array_merge($params, $optParams); 
    $data = $this->__call('get', array($params)); 
    if ($this->useObjects()) { 
    return new Google_DriveFile($data); 
    } else { 
    return $data; 
    } 
} 

它檢查,如果你想使用對象的工作,或不:

$這個 - > useObjects()

你需要在你的api config.php文件中將'use_objects'配置爲'true',它被設置爲'f 'alse'默認。

'use_objects'=>假,

+0

謝謝你 - 我與這個掙扎了一會兒 – user2121620

相關問題