如何使用PHP将演示文稿文件上传到Google幻灯片?

问题描述 投票:1回答:1

如何使用PHP将演示文件(ppt,pptx,pdf)上传到Google幻灯片服务。

我在这些链接中没有找到示例:

https://developers.google.com/slides/quickstart/php

https://developers.google.com/api-client-library/php/support

我的代码:

$service = new Google_Service_Drive($client);

$fileMetadata = new Google_Service_Drive_DriveFile([
    'name' => 'My Presentation',
    'mimeType' => 'application/vnd.google-apps.presentation',
    // 'mimeType' => 'application/vnd.google-apps.document',
]);

$file = $service->files->create($fileMetadata, [
    'data' => file_get_contents(realpath(dirname(__FILE__)) . '/Modelo_Slide_Padrao.pptx'),
    // 'mimeType'   => 'application/vnd.ms-powerpoint', // 'application/pdf',
    'uploadType' => 'multipart',
    'fields' => 'id',
]);
printf("File ID: %s\n", $file->id);

有人帮我吗?

谢谢。

api google-api-php-client
1个回答
3
投票

您无法将演示文稿文件上传到Google幻灯片。您需要执行的操作是使用Google文档类型将文件导入Google云端硬盘。看一下reference documentation,其中有一个如何实现此目的的示例。以下是如何实现您所需的示例。

PPT到Google幻灯片演示:

$service = new Google_Service_Drive($client);

// Create a new file
$file = new Google_Service_Drive_DriveFile(array(
    'name' => 'PPT Test Presentation',
    'mimeType' => 'application/vnd.google-apps.presentation'
));

// Read power point ppt file
$ppt = file_get_contents("SamplePPT.ppt");

// Declare opts params
$optParams = array(
    'uploadType' => 'multipart',
    'data' => $ppt,
    'mimeType' => 'application/vnd.ms-powerpoint'
);

// Import pptx file as a Google Slide presentation
$createdFile = $service->files->create($file, $optParams);

// Print google slides id
print "File id: " . $createdFile->id;

PPTX到Google幻灯片演示文稿:

$service = new Google_Service_Drive($client);

// Create a new file
$file = new Google_Service_Drive_DriveFile(array(
    'name' => 'PPTX Test Presentation',
    'mimeType' => 'application/vnd.google-apps.presentation'
));

// Read Powerpoint pptx file
$pptx = file_get_contents("SamplePPTX.pptx");

// Declare opts params
$optParams = array(
    'uploadType' => 'multipart',
    'data' => $pptx,
    'mimeType' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
);

// Import pptx file as a Google Slide presentation
$createdFile = $service->files->create($file, $optParams);

// Print google slides id
print "File id: " . $createdFile->id;

PDF到Google Document Doc :(对于Google Slide Presentation而言是不可能的)

$service = new Google_Service_Drive($client);

// Create a new file
$file = new Google_Service_Drive_DriveFile(array(
    'name' => 'PDF Test Document',
    'mimeType' => 'application/vnd.google-apps.document'
));

// Read pdf file
$pdf = file_get_contents("SamplePDF.pdf");

// Declare opts params
$optParams = array(
    'uploadType' => 'multipart',
    'data' => $pdf,
    'mimeType' => 'application/pdf'
);

// Import pdf file as a Google Document File
$createdFile = $service->files->create($file, $optParams);

// Print google document id
print "File id: " . $createdFile->id;

每个代码段中唯一更改的是mimeType。对于Mime类型,您可以visit here;对于Google Mime类型,您可以visit here

© www.soinside.com 2019 - 2024. All rights reserved.