我想使用php curl将Google驱动器文件移动到特定文件夹中

时间:2019-03-08 10:53:03

标签: php google-drive-api php-curl google-drive-team-drive

我已上传文件,并使用php curl API在Google驱动器中将其重命名。但是我无法将上传的文件移动到特定的文件夹。请告诉我我的代码在哪里错了?请告诉我应该调用哪个API网址?或其他任何需要更改的代码

$ch = curl_init();
            curl_setopt_array($ch, array(
                CURLOPT_URL => 'https://www.googleapis.com/upload/drive/v3/files?uploadType=media',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type:' . $mime_type, // todo: runtime detection?
                    'Authorization: Bearer ' . $token
                ),
                CURLOPT_POST => 1,
                CURLOPT_POSTFIELDS => file_get_contents($image_location),
                CURLOPT_RETURNTRANSFER => 1
            ));
            $response = curl_exec($ch);
            $id = json_decode($response, TRUE);
            $get_id = $id['id'];
            $link = "https://drive.google.com/file/d/" . $get_id . "/view?usp=sharing";
           curl_close($ch);

            if (isset($id['id'])) {
                $folder_id = 'my_folder_id';
                $get_id = $id['id'];

                $data = array("name" => $position);
                $data_string = json_encode($data);

                $ch2 = curl_init();
                curl_setopt_array($ch2, array(
                    CURLOPT_URL => 'https://www.googleapis.com/drive/v3/files/' . $get_id,
                    CURLOPT_HTTPHEADER => array(
                        'Content-Type:application/json', // todo: runtime detection?
                        'Authorization: Bearer ' . $token
                    ),
                    CURLOPT_POST => 1,
                    CURLOPT_CUSTOMREQUEST => 'PATCH',
                    CURLOPT_POSTFIELDS => $data_string,
                    CURLOPT_RETURNTRANSFER => 1
                ));
                $response = curl_exec($ch2);
                $parsed = json_decode($response, TRUE);
                if ($response === false) {
                    $output = 'ERROR: ' . curl_error($ch2);
                } else {
                    $output = $response;
                }

                // close second request handler  
                curl_close($ch2);
                
                $ch3 = curl_init();
                curl_setopt_array($ch3, array(
                    CURLOPT_URL => 'https://www.googleapis.com/drive/v3/files/' . $get_id,
                    CURLOPT_POST => 1,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_POSTFIELDS => json_encode(array(
                        // Earlier it was title changed to name
                       'addParents' => $folder_id,
                       'removeParents' => 'root',
                    )),
                    // Earlier it was PATCH changed to post
                  CURLOPT_CUSTOMREQUEST => 'PATCH',
                    CURLOPT_HTTPHEADER => array(
                        'Content-Type :application/json',
                        'Authorization: Bearer ' . $token
                    )
                ));
                $response1 = curl_exec($ch3);
                 $parsed1 = json_decode ( $response1, true );
                if ($response1 === false) {
                    $output = 'ERROR: ' . curl_error($ch3);
                } else {
                    $output = $response1;
                }
                print_r($parsed1);
                // close second request handler  
                curl_close($ch3);

我的文件是根目录上载的,但不能在我的文件夹中移动。我已经在Google驱动器中创建了文件夹。 第一次卷曲上传文件 第二次卷曲用于重命名文件 第三次卷曲将文件移动到文件夹

0 个答案:

没有答案