2014-05-25 64 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'应该传递一个布尔值而不是一个字符串。谢谢! –