简体   繁体   English

使用PHP的Google云端硬盘文件列表

[英]Google Drive files list using PHP

I am using PHP and the Google Drive API and need to get files and folder list. 我正在使用PHP和Google Drive API,需要获取文件和文件夹列表。 I want to list out all files in my Google Drive in my web page. 我想在网页上列出Google云端硬盘中的所有文件。

My code is : 我的代码是:

  require_once 'google-api-php-client/src/Google_Client.php';
    require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
    define('STDIN',fopen("php://stdin","r"));
    $client = new Google_Client();
    // Get your credentials from the console
    $client->setClientId('CLIENT_ID');
    $client->setClientSecret('CLIENT_SECRET');
    $client->setRedirectUri('REDIRECT_URI');
    $client->setScopes(array('https://www.googleapis.com/auth/drive'));

    $service = new Google_DriveService($client);

    $authUrl = $client->createAuthUrl();

    //Request authorization
    print "Please visit:\n$authUrl\n\n";
    print "Please enter the auth code:\n";
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for access token
    $accessToken = $client->authenticate($authCode);
    $client->setAccessToken($accessToken);

    retrieveAllFiles($service);

    //foreach($file as $result){
    //  echo $file;
    //}

    function retrieveAllFiles($service) {
    $result = array();
    $pageToken = NULL;

    do {
    try {
      $parameters = array();
      if ($pageToken) {
        $parameters['pageToken'] = $pageToken;
      }
      $files = $service->files->listFiles($parameters);

      $result = array_merge($result, $files->getItems());
      $pageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "An error occurred: " . $e->getMessage();
      $pageToken = NULL;
    }
    } while ($pageToken);

    foreach($files->items as $item)
    {
        echo $item['title'];
    }
    }

How can I get list of file ? 如何获取文件列表? Please help me.... Give me some example code to retrieve all files... 请帮助我。...给我一些示例代码来检索所有文件...

You can check on Files:list docs that contain a working sample with pagination. 您可以检查“ Files:list”文档 ,其中包含带有分页功能的工作示例。 You can either use the DrEdit sample Google Drive app in a bunch of languages. 您可以使用多种语言的DrEdit示例 Google云端硬盘应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM