我不能獲得穩定的劇本時,我嘗試上傳的docx文件,以谷歌驅動器,然後下載文件,但爲PDF。谷歌API,用於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),但與內容長度0.
我做錯了什麼?或者我應該做一些循環,試圖下載文件,直到成功?
'$文件 - > id'沒有這樣的鍵?不應該是'$ file-> data' – pokeybit
'$ service-> files-> create()'返回id對象字段包含在Google雲端硬盤上創建的文件的ID。除了錯誤代碼500' $ file-> id'總是存在。 – Yurciu
也許創建文件和您的導出電話之間的延遲 – pokeybit