2017-02-07 29 views
1

如何使用PHP將演示文稿文件(ppt,pptx,pdf)上傳到Google幻燈片服務。如何使用PHP將演示文件上傳到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); 

有人幫幫我嗎?

謝謝。

+0

Did'nt work! ;( –

+0

哪一個沒有工作?pptx? – Morfinismo

+0

是的。沒有與pptx一起工作。 –

回答

2

您無法將演示文稿上傳到Google幻燈片。您需要做的是使用Google文檔類型將文件導入Google雲端硬盤。看看reference documentation這是一個如何實現這個目標的例子。這裏是如何實現你所需要的例子。

PPT到谷歌幻燈片演示文稿:

$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到谷歌幻燈片演示文稿:

$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到谷歌文檔文件:(不可能谷歌幻燈片演示文稿)

$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

+0

謝謝。是否有可能將pdf,ppt,pptx從驅動器轉換爲谷歌幻燈片? –

+1

上面引用的文檔有一個表格,它解釋了什麼格式可以轉換爲Google幻燈片。從我所看到的,只有Microsoft Powerpoint,這是ppt和OpenDocument演示文稿。我不確定pptx並確信它是 – Morfinismo

+0

Hi @Morfinismo在Google幻燈片API中是否打開ppt或pptx? –

相關問題