使用curl API时,谷歌驱动器上的无标题文件问题

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

当我尝试使用php curl API在谷歌驱动器上传文件时,它提供无标题。我无法显示文件名。如何使用curl php正确上传文件?我不想使用客户端库。谷歌驱动器上没有上传文件的文件标题

function save_application_form($wpcf7) {

//global $wpdb;
    $submission = WPCF7_Submission::get_instance();

    if ($submission) {
        $submited = array();
        $submited['title'] = $wpcf7->title();
        $submited['posted_data'] = $submission->get_posted_data();
        $uploaded_files = $submission->uploaded_files();
    }
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $cf7_file_field_name = 'file-846';
    $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,
            'Authorization: Bearer ' . $token
        ),
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => file_get_contents($image_location),
        CURLOPT_RETURNTRANSFER => 1
    ));
    $response = curl_exec($ch);
    $err = curl_error($ch);

    curl_close($ch);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
}
php google-api google-drive-sdk filenames php-curl
1个回答
0
投票

您没有上传仅上传文件本身的文件的元数据。 post body需要包含文件元数据,文件名是这些项目之一。

官方php客户端库示例

documentation演示了如何使用googles客户端库执行此操作。

$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => 'photo.jpg'));
$content = file_get_contents('files/photo.jpg');
$file = $driveService->files->create($fileMetadata, array(
    'data' => $content,
    'mimeType' => 'image/jpeg',
    'uploadType' => 'multipart',
    'fields' => 'id'));
printf("File ID: %s\n", $file->id);

PHP CURL狂野猜测

对不起,我不能帮你做卷毛。但它应该只是将帖子正文作为包含您希望的数据的JSon字符串发送。以下是一些谷歌搜索之后的最佳猜测。

$data = array("name" => "picture.jpg", "mimeType"=> "image/jpeg");                                                                    
$data_string = json_encode($data);  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);   
© www.soinside.com 2019 - 2024. All rights reserved.