我想使用Google Drive API curl

时间:2019-03-05 09:56:20

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

我已使用curl API将文件上传到Google驱动器。但是我想将文件上传到特定的文件夹。我也尝试过在API网址中使用文件夹ID,例如:https://www.googleapis.com/upload/drive/v3/files/folder_id?uploadType=media

add_action('wpcf7_before_send_mail', 'save_application_form');

function save_application_form($WPCF7_ContactForm) {

    $wpcf7 = WPCF7_ContactForm :: get_current();
    $submission = WPCF7_Submission :: get_instance();
    if ($WPCF7_ContactForm->id == 8578) {
        if ($submission) {
            $submited = array();
            //$submited['title'] = $wpcf7->title();
            $submited['posted_data'] = $submission->get_posted_data();
            $uploaded_files = $submission->uploaded_files();
            $name = $submited['posted_data']["Name"];
            $position_test = $submited['posted_data']["Position"];
            $email = $submited['posted_data']["Email"];
            $phone = $submited['posted_data']["phone"];
            $message = $submited['posted_data']["Message"];
            $position = $submited['posted_data']["AttachYourCV"];

           // $test2 = $_FILES['AttachYourCV']['name'];

            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $cf7_file_field_name = 'AttachYourCV';

            $image_location = $uploaded_files[$cf7_file_field_name];
            $mime_type = finfo_file($finfo, $image_location);
            $token = GetRefreshedAccessToken('client_id', 'refresh_token', 'client_secret');
            $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'];

    curl_close($ch);
   if (isset($id['id'])) {
            $get_id = $id['id'];
            $post_fields = array();
            $folder_id = '1-9r8oVsAfV_iJmYh1cZYPMvE9Qhv8RLA';
            // remove extension  
            $this_file_name = explode('.', $position);

            // submit name field  
            $post_fields['name'] .= $this_file_name[0];
            $post_fields['parents'] = $folder_id[1];
            $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 => json_encode($post_fields),
                CURLOPT_RETURNTRANSFER => 1
            ));
            $response = curl_exec($ch2);
            if ($response === false) {
                $output = 'ERROR: ' . curl_error($ch2);
            } else {
                $output = $response;
            }

            // close second request handler  
            curl_close($ch2);


        }
}

我添加了另一个curl调用,但仍未在文件夹中获取文件。这样,我的文件无标题问题就解决了。

1 个答案:

答案 0 :(得分:0)

将文件上传到Google驱动器是两部分的事情。 (请注意,可以将其作为一个调用来完成,但如果要添加corect数据,则最好将其作为两个来完成)

create添加文件的初始元数据。名称,媒体类型和位置(作为其父目录)。

第二次实际上传文件本身。

注意:未先发送元数据就上传的文件通常会创建一个虚拟的元数据,其文件标题为“未知”。

创建

当您对惯性元数据进行create时。您需要在此初始调用中添加父母,该字段称为parents[],您需要在其中添加文件ID。我无法帮助您完成此操作,因为您没有为其添加代码。

上传

默认情况下,除非您为文件添加父文件夹ID,否则文件会上传到根文件夹。

如果您选中documentation,则会找到可选的查询参数

enter image description here

尝试使用&addParents=[folderId]

示例:

https://www.googleapis.com/upload/drive/v3/files?uploadType=media&addParents=1bpHmln41UI-CRe5idKvxrkIpGKh57T32

如果您最初在根目录中创建了文件的元数据,然后尝试将文件上传到其他目录,我怀疑上传会创建新的元数据。这两个调用需要相同。您的作品应设置父母。

帖子正文

$data = array("name" => "test.jpg", "parents" => array("123456"));                                                                    
$data_string = json_encode($data);                                                                                   

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);