Library PHP sederhana untuk berinteraksi dengan Google Drive API v3, mendukung:
- OAuth2 Authorization Code Flow
- Refresh Access Token otomatis
- Membuat folder
- Upload file kecil (multipart)
- Upload file besar (resumable / chunked)
- Download file
- Mendapatkan metadata file lengkap
- Membuat dan menghapus share link (permissions)
- Mendapatkan URL file siap pakai di
<img>atau<video> - Penanganan error terpusat
- Kompatibel dan siap dipublish via Composer
Instalasi melalui composer:
composer require koyabu/googledriveapi- Buka https://console.developers.google.com/
- Buat project baru
- Aktifkan Google Drive API
- Buat OAuth 2.0 Client ID
- Atur Redirect URI, contoh:
https://example.com/drive/callback - Catat Client ID dan Client Secret
use Koyabu\GoogleDriveApi\GoogleDriveClient;
$drive = new GoogleDriveClient([
'client_id' => 'YOUR_CLIENT_ID',
'client_secret' => 'YOUR_CLIENT_SECRET',
'redirect_uri' => 'https://example.com/drive/callback',
]);echo $drive->getAuthUrl();User akan login dan mendapatkan authorization code.
$token = $drive->fetchAccessToken($_GET['code']);
// Simpan access token & refresh token$result = $drive->createFolder('Backup');
if (!$result) {
echo $drive->lastError;
}$drive->uploadFile(__DIR__.'/file.txt', 'file.txt', $folderId); // folderId optional$drive->uploadLargeFile(__DIR__.'/video.mp4', 'video.mp4', $folderId); // folderId optional$drive->downloadFile($fileId, __DIR__.'/downloaded_video.mp4');$info = $drive->fileInfo($fileId);
print_r($info);- Termasuk
webViewLinkdanwebContentLinksiap pakai untuk<img>atau<video>
$publicUrl = $drive->getFileUrl($fileId, 'content'); // 'view' atau 'content''view'→ webViewLink (preview)'content'→ webContentLink (raw download, bisa langsung di<img>atau<video>)
Contoh penggunaan di <img> atau <video>:
echo '<img src="' . $publicUrl . '" alt="Image">';echo '<video controls><source src="' . $publicUrl . '" type="video/mp4"></video>';$permission = $drive->createShareLink($fileId, 'reader', 'anyone');
$drive->removeShareLink($fileId, $permissionId);- Pastikan timezone dan server clock sinkron.
- Refresh token harus disimpan secara permanen (database/file).
- Access token bisa berubah setelah refresh.
- Untuk file besar, gunakan
webContentLinklangsung di<video>tag untuk streaming. fileInfoRaw()hanya untuk file kecil (<2MB) agar aman untuk memori.getFileUrl()helper memudahkan embed file di web.
/
│── src/
│ └── GoogleDriveClient.php
│── composer.json
│── README.md
│── LICENSE
{
"name": "koyabu/googledriveapi",
"description": "Google Drive API Client",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Koyabu\\Googledriveapi\\": "src/"
}
},
"authors": [
{
"name": "Stieven Kalengkian",
"email": "stieven.kalengkian@gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"google/apiclient": "^2.0"
}
}Pull request, bug report, dan perbaikan sangat diterima.
MIT License atau sesuai kebutuhan Anda.