如何将RTF文档转换为Google文档?

时间:2014-05-25 16:21:38

标签: php google-api-php-client

我的Google示例代码使用了我的应用程序创建的RTF文件。如何将此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 个答案:

答案 0 :(得分: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示例,但不幸的是,它使用了旧的客户端库。我被告知他们正在更新它。可以在GitHub上找到新的Google API PHP客户端库。那里有一个文件上传示例,但它非常基本。 Fileupload.php并且不包括convert参数。