2014-05-25 79 views
2

我已經得到了Google的示例代碼與我的應用程序創建的RTF文件一起工作。如何將此RTF文檔轉換爲Google文檔?如何將RTF文檔轉換爲Google文檔?

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

$client = new Google_Client(); 
// Get your credentials from the console 
$client->setClientId('xxxxxx'); 
$client->setClientSecret('xxxxxx'); 
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive')); 

$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); 

//Insert a file 
$file = new Google_DriveFile(); 
$file->setTitle('5.25.14-M02000-Ramin'); 
$file->setDescription('A test document'); 
$file->setMimeType('application/rtf'); 

$data = file_get_contents('5.25.14-M02000-Ramin.rtf'); 

$createdFile = $service->files->insert($file, array(
     'data' => $data, 
     'mimeType' => 'application/rtf', 
    )); 

print_r($createdFile); 
?> 

回答

1

您需要根據您的要求發送optional parameter

convert boolean 
Whether to convert this file to the corresponding Google Docs format. (Default: false) 

可能是這樣的,但我不確定你的代碼是使用舊的Google API PHP客戶端Lib。

$createdFile = $service->files->insert($file, array(
     'data' => $data, 
     'mimeType' => 'application/rtf', 
     'convert' => 'true' 

    )); 

注:是的,我知道你是以下Files: insert的例子,但不幸的是,它使用舊的客戶端庫。我被告知他們正在更新它。新的Google API PHP客戶端庫位於GitHub。這裏還有一個文件上傳的例子,但它非常基本。 Fileupload.php,並且不包含convert參數。

+0

@達爾姆,你是對的。唯一的是''convert'=> true'應該傳遞一個布爾值而不是一個字符串。謝謝! –