如何在谷歌共享驱动器上的文件夹之间移动文件

问题描述 投票:0回答:1

我编写了以下函数来在 Google 共享驱动器中的文件夹之间移动文件。它工作正常,没有任何错误,但文件没有移动。我该怎么办?

    public function MoveFileToFolder($access_token, $file_id, $source_folder_id, $target_folder_id) {
        $apiURL = self::DRIVE_FILES_URI . $file_id . "?corpora=allDrives&includeItemsFromAllDrives=true&supportsAllDrives=true";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $apiURL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token));

        $move_data = [
            'addParents' => $target_folder_id,
            'removeParents' => $source_folder_id,
        ];

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($move_data));

        $data = json_decode(curl_exec($ch), true);

        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

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

        return $data;
    }

我会移动文件,调试显示

File_id: 1vN8eC8XolWEc0OtpFUY2PBewfh5O9VqG
Source_parent_id: 12QqaPU5DEqGDO6csMy-kjZYS0dmKJfW7
Target_parent_id: 1D-2_ZDYGRDgXgHAfK9nOQqzfYBo3fXIb
Query: https://www.googleapis.com/drive/v3/files/1vN8eC8XolWEc0OtpFUY2PBewfh5O9VqG?corpora=allDrives&includeItemsFromAllDrives=true&supportsAllDrives=true
Response HTTP Code: 200
Response Data: Array
(
    [kind] => drive#file
    [id] => 1vN8eC8XolWEc0OtpFUY2PBewfh5O9VqG
    [name] => Firme presenze anno 2023 Accoglienza maschile Settembre IACUITTO DANIELE.pdf
    [mimeType] => application/pdf
    [teamDriveId] => 0AB8c-2VqWymEUk9PVA
    [driveId] => 0AB8c-2VqWymEUk9PVA
)

如何功能正常,但文件没有移动。所有参数都正确,我是拥有完全权限的管理员

php json google-drive-api php-curl
1个回答
0
投票

我自己解决了。 这是正确的函数:

    public function MoveFileToFolder($access_token, $file_id, $source_folder_id, $target_folder_id) {
    $apiURL = self::DRIVE_FILES_URI . $file_id . "?addParents=$target_folder_id&removeParents=$source_folder_id&supportsAllDrives=true";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
    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);
    curl_close($ch);

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

    return $data["driveId"];
}
© www.soinside.com 2019 - 2024. All rights reserved.