Hot to get the list of folders in google drive using php and curl

Joined
Oct 10, 2023
Messages
2
Reaction score
0
Dear Team,

Is it possible to get the list of folders in my drive using php and curl? I have been trying this but all to no avail.
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Here is the answer from GPT, maybe it will be useful for you.

To get the list of folders in Google Drive using PHP and cURL, you will need to interact with the Google Drive API. Here's a step-by-step guide on how to do this:

  1. Set up a project in the Google Cloud Console and enable the Google Drive API:
    a. Go to the Google Cloud Console (https://console.cloud.google.com/). b. Create a new project or select an existing one. c. In the project dashboard, click on "Enable APIs and Services." d. Search for "Google Drive API" and click on it. e. Click the "Enable" button to enable the API.
  2. Create OAuth 2.0 credentials:
    a. In the Google Cloud Console, navigate to "APIs & Services" -> "Credentials." b. Click the "Create credentials" button and choose "OAuth client ID." c. Configure the OAuth consent screen with the required information. d. Select "Web application" as the application type. e. Add the authorized JavaScript origins and redirect URIs for your PHP application. f. Click "Create" to create the OAuth client ID. g. Download the JSON file containing your client ID and client secret.
  3. Install the required PHP libraries using Composer:
    You'll need to install the Google API client library for PHP. You can do this using Composer:

    Code:
    composer require google/apiclient:^2.0

  4. Create a PHP script to fetch the list of folders using cURL and the Google Drive API:

    PHP:
    <?php
    require_once 'vendor/autoload.php'; // Include the Google API client library
    
    // Replace with your OAuth 2.0 credentials file
    $credentialsPath = 'path/to/your/credentials.json';
    
    // Initialize the Google API client
    $client = new Google_Client();
    $client->setAuthConfig($credentialsPath);
    $client->addScope(Google_Service_Drive::DRIVE_READONLY);
    
    // Authorize the client
    if ($client->isAccessTokenExpired()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    }
    
    // Create the Google Drive service
    $driveService = new Google_Service_Drive($client);
    
    // List folders
    $optParams = array(
        'q' => "mimeType='application/vnd.google-apps.folder'", // Filters for folders
    );
    $results = $driveService->files->listFiles($optParams);
    
    // Print folder names
    if (count($results->getFiles()) == 0) {
        echo 'No folders found.';
    } else {
        echo 'Folders:' . PHP_EOL;
        foreach ($results->getFiles() as $file) {
            echo $file->getName() . PHP_EOL;
        }
    }

    Make sure to replace 'path/to/your/credentials.json' with the path to the JSON file containing your OAuth 2.0 credentials.
  5. Run the PHP script, and it will fetch and display the list of folders in your Google Drive.
Ensure that you have the necessary permissions and that your OAuth 2.0 credentials are correctly configured in the Google Cloud Console for this script to work.
 
Joined
Oct 10, 2023
Messages
2
Reaction score
0
Here is the answer from GPT, maybe it will be useful for you.
I already have a project created and feedback also specify and what it does is to upload a file into google drive using curl and php. Now can i still use the same project to get the list of folder or i will need to create different project again to get the list of folders using php and curl.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top