How to upload presentation file into Google Slides using PHP?

余生长醉 提交于 2019-12-08 05:50:55

问题


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

My code:

$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);

Somebody help me?

Thank you.


回答1:


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 to Google Slides Presentation:

  $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 to Google Slides Presentation:

  $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 to Google Document Doc: (is not possible to 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'
  ));

  $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;

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.



来源:https://stackoverflow.com/questions/42100147/how-to-upload-presentation-file-into-google-slides-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!