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

时间:2017-02-07 21:15:53

标签: api google-api-php-client

如何使用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);

有人帮助我吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

您无法将演示文稿文件上传到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'
  ));

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

  //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'
  ));

  $pptx = file_get_contents("SamplePPTX.pptx"); // read power point pptx file

  //declare opts params
  $optParams = array(
    'uploadType' => 'multipart',
    'data' => $ppt,
    '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'
  ));

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

  //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