2017-07-28 15 views
1

docxファイルをGoogleドライブにアップロードしてからPDFとしてダウンロードすると、安定したスクリプトを取得できません。Google Api for PHP(ドライブAPI).pdfとしてアップロード.docxファイル

コード:

//Google API 
require_once('vendor/autoload.php'); 

putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/2ab4ece19bd5.json'); 
$client = new Google_Client(); 
$client->setApplicationName('sp-gen'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive')); 
$client->useApplicationDefaultCredentials(); 
$service = new Google_Service_Drive($client); 

$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => '281e2399740c88957143507721bd0f25.docx', 
    'mimeType' => 'application/vnd.google-apps.document' 
)); 

$content = file_get_contents('281e2399740c88957143507721bd0f25.docx'); 

$file = $service->files->create($fileMetadata, array(
    'data' => $content, 
    'mimeType' => 'application/vnd.google-apps.document', 
    'uploadType' => 'multipart', 
    'fields' => 'id') 
); 

$content = $service->files->export($file->id, 'application/pdf', array('alt' => 'media')); 
file_put_contents(str_replace('.docx', '.pdf', '281e2399740c88957143507721bd0f25.docx'), $content->getBody()->getContents()); 

このコードは、用途の.. 20から30パーセントで動作します。場合によっては$ service-> files-> export()がエラーコード500を返しますが、多くの場合、通常の応答(200)を返しますが、Content-Lengthは0になります。

何か問題が起こっていますか?それとも、成功するまでファイルをダウンロードしようとしているのですか?

+0

'$ファイル - >? '$ file-> data'であるべきではありません。 – pokeybit

+0

' $ service-> files-> create() '戻り値のオブジェクトidフィールドには、Googleドライブで作成されたファイルのIDが含まれています。エラーコード500を除く '$ file-> id'は常に存在します。 – Yurciu

+0

ファイルの作成とエクスポートの呼び出しの間に遅延がある可能性があります – pokeybit

答えて

0

OK。だから私は解決策を探して二日間を過ごすと、私はいくつかの結論を思い付いた:

  1. サーバ間の認証を使用して、「別の」のGoogleドライブアカウントですと、サービスアカウントを作成する必要があります。つまり、スクリプトを使ってファイルを作成すると、サービスアカウントにファイルが作成され、APIを介してのみこのファイルにアクセスできます。
  2. 実際のGoogleアカウントをファイルに接続する権限をファイルに作成し、Googleドライブからそのファイルにアクセスすることができます。現時点でGoogle PHP APIには適切な方法がないため、サービスアカウントからGoogleアカウントへの所有権をファイルに転送することはできません。 これを設定する方法はわかりません。Class summary.

  3. キーはExponential backoffを使用します。つまり、成功しないと試してみる。 (͡°͜ʖ͡°)

CODE:いいえ、そのようなキーid`

//Google API 
require_once('vendor/autoload.php'); 

putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/2ab4ece19bd5.json'); 
$client = new Google_Client(); 
$client->setApplicationName('sp-gen'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive')); 
$client->useApplicationDefaultCredentials(); 
$service = new Google_Service_Drive($client); 

$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => '281e2399740c88957143507721bd0f25.docx', 
    'mimeType' => 'application/vnd.google-apps.document' 
)); 

$content = file_get_contents('281e2399740c88957143507721bd0f25.docx'); 

$file = $service->files->create($fileMetadata, array(
    'data' => $content, 
    'uploadType' => 'multipart' 
); 

//Create new permission 
$newPermission = new Google_Service_Drive_Permission(); 

//set email of account, that will have access to file. 
$newPermission->setEmailAddress('<email here>'); 

//Must be user or group, if you pass email adress 
// user | group | domain | anyone 
$newPermission->setType('user'); 

//Could be owner but I can not set transferOwnership 
// organizer | owner | reader | writer 
$newPermission->setRole('writer'); 

$service->permissions->create($file->getId(), $newPermission); 

$file_name = str_replace('.docx', '.pdf', '281e2399740c88957143507721bd0f25.docx'); 

$attempt = 1; 
do{ 
    //Wait 5000ms 
    usleep(500000*$attempt); 

    //Try to get pdf file. 
    $content = $service->files->export($file->getId(), 'application/pdf', array('alt' => 'media')); 

    //Save just fetched data. 
    file_put_contents($file_name, $content->getBody()->getContents()); 

    if(filesize($file_name)) break; 
    else $attempt++; 

}while(true); 
+0

バックオフは線形で指数関数ではありません。 – pinoyyid

関連する問題