且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-14 09:02:08

您无法将演示文稿文件上传到 Google 幻灯片.您需要做的是使用 Google Doc Type 将文件导入 Google Drive.查看参考文档strong> 其中有一个如何实现这一点的例子.以下是如何实现您所需要的示例.

You cannot upload a presentation file to Google Slides. What you are required to do is to import the file to Google Drive using a Google Doc Type. Take a look at the reference documentation which has an example of how to achieve this. Here are the examples of how to achieve what you need.

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 optional parameters
$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 文档文档:(不能用于 Google 幻灯片演示)

$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 类型的参考,您可以访问此处和有关 Google Mime 类型的参考,您可以访问此处.

The only thing that changes in each code snippet is the mimeType. For a reference of Mime Types you can visit here and for a reference of Google Mime Types you can visit here.