php 如何验证文件夹是否存在于谷歌共享驱动器

dy2hfwbg  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(107)

我写了下面的函数来检查Google Drive中是否存在文件夹。它工作正常,但如果文件夹是在共享驱动器,它不工作。我该怎么办?

public function ExistsFolderOnDrive($access_token, $folder_name, $parent_folder_id) {

        $query = "mimeType='application/vnd.google-apps.folder' and trashed=false and name='$folder_name' and '$parent_folder_id' in parents";

        $apiURL = self::DRIVE_FILES_URI . "?q=" . urlencode($query);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $apiURL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token));

        $data = json_decode(curl_exec($ch), true);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        if ($http_code != 200) {
            $error_msg = 'Failed to retrieve folder from Google Drive';
            if (curl_errno($ch)) {
                $error_msg = curl_error($ch);
            }
            throw new Exception('Error ' . $http_code . ': ' . $error_msg);
        }

        if (empty($data['files'])) {
            // The folder doesn't exist 
            return false;
        } else {
            // Folder exists, return folder information
            return $data['files'][0]['id'];
        }
    }

我试着加上

'sharedWithMe' in parents

或甚至

and ('$parent_folder_id' in parents or 'sharedWithMe' in parents)

但它不起作用。我正在寻找一些关于如何做这件事的建议

zzzyeukh

zzzyeukh1#

关于你下面的问题,
它工作正常,但如果文件夹是在共享驱动器,它不工作。我该怎么办?
在您的显示脚本的情况下,为了搜索共享驱动器,如何修改它如下?

发件人:

$apiURL = self::DRIVE_FILES_URI . "?q=" . urlencode($query);

收件人:

$apiURL = self::DRIVE_FILES_URI . "?q=" . urlencode($query) . "&corpora=allDrives&includeItemsFromAllDrives=true&supportsAllDrives=true";

参考资料:

相关问题