Install Google APIs Client Library for PHP
for more info check https://github.com/googleapis/google-api-php-client
1 |
composer require google/apiclient:"^2.0" |
Authentication
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
for laravel $client = new \Google_Client(); $client->setClientId('client_id'); $client->setClientSecret('client_secret'); $client->addScope("https://www.googleapis.com/auth/drive.file"); $client->setRedirectUri('redirect_url'); // offline access will give you both an access and refresh token so that // your app can refresh the access token without user interaction. $client->setAccessType('offline'); // Using "consent" ensures that your application always receives a refresh token. // If you are not using offline access, you can omit this. // $client->setApprovalPrompt("consent"); //$client->setIncludeGrantedScopes(true); // incremental auth $auth_url = $client->createAuthUrl(); // generating authentication url |
after run authentication url you will get authorization code for an access token
1 2 |
$client->authenticate("code"); $access_token = $client->getAccessToken(); // generating access_token with refresh token |
you can save access_token data in your database and now update access_token use below code
1 |
$new_access_token = $client->refreshToken($tokendata['refresh_token']); // generating new access_token |
Check google drive folder and store a file with different url
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 |
$service = new \Google_Service_Drive($client); $foldername = "/main_folder/sub_folder/"; $folder_names = explode("/",$foldername); $i=0; $service = new \Google_Service_Drive($client); foreach($folder_names as $folder){ if(!empty($folder)){ if(isset($new_folder->id)){ $parent = array($new_folder->id); $query = array('q'=>"name='".$folder."' and mimeType='application/vnd.google-apps.folder' and trashed=false and parents='".$new_folder->id."'"); }else{ $parent = array(); $query = array('q'=>"name='".$folder."' and mimeType='application/vnd.google-apps.folder' and trashed=false"); } $drive_folders = $service->files->listFiles($query); if(count($drive_folders)>0){ foreach($drive_folders as $drive_folder){ $new_folder = $drive_folder; } }else{ $fileMetadata = new \Google_Service_Drive_DriveFile(array( 'name' => $folder, 'parents' => $parent, 'mimeType' => 'application/vnd.google-apps.folder')); // Creating Folder with given Matadata and asking for ID field as result $new_folder = $service->files->create($fileMetadata, array('fields' => 'id')); } } $i++; } if(isset($new_folder->id)){ $parent = array($new_folder->id); }else{ $parent = array(); } $fileMetadata = new \Google_Service_Drive_DriveFile(array('name' => $filename,'parents' => $parent)); $file = $service->files->create($fileMetadata, array( 'data' => $image_file_data, // this is file data 'mimeType' => 'application/octet-stream', 'uploadType' => 'multipart', 'fields' => 'id')); |
Using Function in laravel
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 98 99 100 101 102 103 104 105 106 107 108 109 |
// $filedata is file content data // $localfilepath is where your file store in localsystem $GoogleDriveUploaded = Upload_File_Google_Drive($client,$foldername,$filename,$filedata,$localfilepath); function readFileChunk($handle, $chunkSize) { $byteCount = 0; $giantChunk = ""; while (!feof($handle)) { // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file $chunk = fread($handle, 8192); $byteCount += strlen($chunk); $giantChunk .= $chunk; if ($byteCount >= $chunkSize) { return $giantChunk; } } return $giantChunk; } // upload file in google drive function Upload_File_Google_Drive($client,$foldername,$filename,$image_data,$file_path){ $mime_type = mime_content_type($file_path); $file = file_get_contents($file_path); $fileSize = strlen($file); $folder_names = explode("/",$foldername); $client->setDefer(false); $i=0; $service = new \Google_Service_Drive($client); foreach($folder_names as $folder){ if(!empty($folder)){ if(isset($new_folder->id)){ $parent = array($new_folder->id); $query = array('q'=>"name='".$folder."' and mimeType='application/vnd.google-apps.folder' and trashed=false and parents='".$new_folder->id."'"); }else{ $parent = array(); $query = array('q'=>"name='".$folder."' and mimeType='application/vnd.google-apps.folder' and trashed=false"); } $drive_folders = $service->files->listFiles($query); if($drive_folders){ if(count($drive_folders)>0){ foreach($drive_folders as $drive_folder){ $new_folder = $drive_folder; } }else{ $fileMetadata = new \Google_Service_Drive_DriveFile(array( 'name' => $folder, 'parents' => $parent, 'mimeType' => 'application/vnd.google-apps.folder')); // Creating Folder with given Matadata and asking for ID field as result $new_folder = $service->files->create($fileMetadata, array('fields' => 'id')); } } } $i++; } if(isset($new_folder->id)){ $parent = array($new_folder->id); }else{ $parent = array(); } $fileMetadata = new \Google_Service_Drive_DriveFile(array('name' => $filename,'parents' => $parent)); if($fileSize > 4194304){ $chunkSizeBytes = 1 * 1024 * 1024; $client->setDefer(true); $request = $service->files->create($fileMetadata); $media = new Google_Http_MediaFileUpload( $client, $request, $mime_type, null, true, $chunkSizeBytes ); $media->setFileSize(filesize($file_path)); $status = false; $handle = fopen($file_path, "rb"); while (!$status && !feof($handle)) { // read until you get $chunkSizeBytes from TESTFILE // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file // An example of a read buffered file is when reading from a URL $chunk = readFileChunk($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk); } // The final value of $status will be the data from the API for the object // that has been uploaded. $result = false; if ($status != false) { $result = $status; } fclose($handle); return $result; }else{ $file = $service->files->create($fileMetadata, array( 'data' => $image_data, 'mimeType' => 'application/octet-stream', 'uploadType' => 'multipart', 'fields' => 'id')); return $file; } } |
Leave a reply