-2
<?php

$id = '0B475ByfcR9n4a1JMVEZxQno2Tmc';

$ch = curl_init('https://drive.google.com/uc?id='.$id.'&export=download');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, []);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));                                                                                                                                                                          
$result = curl_exec($ch);

$object = json_decode(str_replace(')]}\'', '', $result));

exit(header('Location: '. $object->downloadUrl));

I have tried to generate google drive direct download link without opening web page successfully with this code but it's not working today:

  • When I copy/paste your code I get a `400 Bad Request`. – Anthony Mar 13 '18 at 10:20
  • [`400 Bad Request`](https://developers.google.com/drive/v3/web/handle-errors#400_bad_request) means that a required field or parameter has not been provided, the value supplied is invalid, or the combination of provided fields is invalid. Drive also supports providing users direct access to a file via the URL in the [`webViewLink`](https://developers.google.com/drive/v3/web/manage-downloads) property. You may check this link as a reference: https://stackoverflow.com/questions/47625214/generate-custom-direct-download-link-for-google-drive-link?noredirect=1&lq=1. – abielita Mar 13 '18 at 16:25

2 Answers2

0

google drive video sharing link to download link convert

$download_link = preg_replace('/\/file\/d\/(\w+)\/view\?usp=sharing/',"/uc?export=download&id=$1", $sharing_file_name)
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
Parasp2008
  • 21
  • 3
  • 2
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Filnor Apr 18 '18 at 06:05
  • Replace regex with this to include other usp parameters (like drivesdk) : `\/file\/d\/(\w+)\/view\?(.+)` – Arnooo Apr 17 '21 at 08:36
0
<?php 

$id = '1IE2K47kVpcIUpLXDjtOcveiTnH9d4PUR';

$kntl = 'https://drive.google.com/uc?export=download&confirm=Ss_7&id='.$id.'';


    function downlah($url){
     $data = curl_init();
     curl_setopt($data, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($data, CURLOPT_URL, $url);
     $hasil = curl_exec($data);
     curl_close($data);
     return $hasil;
}
$dnanaoid =  downlah($kntl);

 $lnacoisncis = explode('<A HREF="', $dnanaoid);

$sdcpajpo = explode('">', $lnacoisncis[1]);


echo $directlink = $sdcpajpo[0]; //DIRECT LINK
?>