-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshorten_drive.php
97 lines (70 loc) · 2.51 KB
/
shorten_drive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/*
@Author : Ahmad Hegazy <[email protected]> <hegazy.ml>
@description : SHORTEN GOOGLE DRIVE FOLDERS INSIDE A FOLDER
@other info:
//YOU NEED TO CREATE AND APP AT GOOGLE CONSOLE : https://console.developers.google.com/apis
//IT NEEDS GOOGLE DRIVE PERMISSION AND GOOGLE SHORTEN URL PERMISSION FOR THE APP YOUR ARE USING IT'S KEY .
//READS IT AS A GET REQUEST id=[FOLDER ID]
//shorten_drive.php?id=[FOLDER ID]
*/
header('Content-Type: text/html; charset=utf-8');
//Don't forget to insert your api key here..
$key="YOUR GOOGLE API KEY HERE";
function grab_data($id){
global $key;
$url="https://www.googleapis.com/drive/v2/files?q=%27" . $id . "%27+in+parents&key=" . $key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$json= curl_exec($ch);
$json = str_replace('\n',"<br/>",$json);
$data = json_decode($json,TRUE,JSON_UNESCAPED_UNICODE);
if(json_last_error() != JSON_ERROR_NONE){
die("ERROR PLEASE CHECK YOUR API KEY ..");
}
$items = $data['items'];
foreach($items as $item){
$type=$item['mimeType'];
if(strpos($type, 'folder') !== false){
$plink=$item['alternateLink'];
$title=$item['title'];
$id=$item['id'];
$short = shortLnk($plink);
echo "FOLDER $title : " . $short['id'];
echo "<br/>";
grab_data($id);
}
}
}
function shortLnk($url){
global $key;
$postData = array('longUrl' => $url);
$curlObj = curl_init();
$jsonData = json_encode($postData);
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key=' . $key);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
//change the response json string to object
$json = json_decode($response,TRUE,JSON_UNESCAPED_UNICODE);
curl_close($curlObj);
if(json_last_error() != JSON_ERROR_NONE){
die("ERROR PLEASE CHECK YOUR API KEY ..");
}
return $json;
}
if(isset($_GET['id'])){
$short = shortLnk('https://drive.google.com/open?id=' . $_GET['id']);
echo "FOLDER :" . $short['id'];
echo "<br/>";
grab_data($_GET['id']);
}else{
die('ERROR Needs folder id > shorten_drive.php?id=FOLDERID');
}
?>