Skip to content

Instantly share code, notes, and snippets.

@ariakm25
Created April 8, 2019 05:01
Show Gist options
  • Save ariakm25/31ab8e501fe749dfff920670888f017b to your computer and use it in GitHub Desktop.
Save ariakm25/31ab8e501fe749dfff920670888f017b to your computer and use it in GitHub Desktop.
<?php
//Fungsi untuk merubah format bytes
function filesize_formatted($file)
{
$bytes = $file;
if ($bytes >= 1073741824) {
return number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
return number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
return number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 1) {
return $bytes . ' bytes';
} elseif ($bytes == 1) {
return '1 byte';
} else {
return '0 bytes';
}
}
//Fungsi untuk mendapatkan direct url
function generateLink($DriveID, $direct=FALSE) {
$ch = curl_init("https://drive.google.com/uc?id=$DriveID&authuser=0&export=download");
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => [],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => 'gzip,deflate',
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_HTTPHEADER => [
'accept-encoding: gzip, deflate, br',
'content-length: 0',
'content-type: application/x-www-form-urlencoded;charset=UTF-8',
'origin: https://drive.google.com',
'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
'x-client-data: CKG1yQEIkbbJAQiitskBCMS2yQEIqZ3KAQioo8oBGLeYygE=',
'x-drive-first-party: DriveWebUi',
'x-json-requested: true'
]
));
$response = curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($response_code == '200') { // Jika response status OK
$object = json_decode(str_replace(')]}\'', '', $response));
if(isset($object->downloadUrl)) {
if($direct) return header("Location:$object->downloadUrl");
return [
'link' => $object->downloadUrl,
'name' => $object->fileName,
'size' => filesize_formatted($object->sizeBytes)
];
}
} else {
return $response_code;
}
}
//mengambil query url
$url = $_GET['url'];
echo json_encode(generateLink($url));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment