如何使用PHP将演示文稿文件上传到Google幻灯片? [英] How to upload presentation file into Google Slides using PHP?
问题描述
如何使用PHP将演示文件(ppt,pptx,pdf)上传到Google幻灯片服务.
How can I upload presentation file (ppt, pptx, pdf) to Google Slides service using PHP.
我没有在这些链接中找到示例:
I didn't find an example in these links:
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);
有人帮我吗?
谢谢.
推荐答案
您无法将演示文稿文件上传到Google幻灯片.您需要执行的操作是使用Google文档类型将文件导入Google云端硬盘.看看 参考文档 其中有一个如何实现此目的的示例.以下是如何实现所需需求的示例.
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 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类型的参考,您可以访问此处,然后有关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.
这篇关于如何使用PHP将演示文稿文件上传到Google幻灯片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!