如何使用 php-curl 以 "multipart" 类型将文件上传到 Google 云端硬盘?

How can I upload files to GoogleDrive in "multipart" type by using php-curl?

我想使用 php curl 而非 google-api-php-client, but I really don't know how to do it, here is the documentation:Send a multipart upload request

上传

这是我的代码片段,我卡在了 CURLOPT_POSTFIELDS,有人可以帮助我吗?

public function uploadByCurl($uploadFilePath, $accessToken){
    $ch = curl_init();
    $mimeType = $this->getMimeType($uploadFilePath);
    $options = [
        CURLOPT_URL =>  'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => [
            'file' => new \CURLFile($uploadFilePath),
            // 'name' =>
        ],
        CURLOPT_HTTPHEADER => [
            'Authorization:Bearer ' . $accessToken,
            'Content-Type:' . $mimeType,
            'Content-Length:' . filesize($uploadFilePath),
        ],
        //In case you're in Windows, sometimes will throw error if not set SSL verification to false
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => 0,
    ];
    //In case you need a proxy
    //$options[CURLOPT_PROXY] = 'http://127.0.0.1:1087';

    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    return $result;
}

我只是不知道如何将其翻译成代码(不熟悉multipart/related):

  • 您想使用 multipart/ralated 和 Drive API v3.
  • 上传文件
  • 您想使用 PHP CURL 实现此目的。
  • 您的访问令牌可用于将文件上传到 Google 云端硬盘。

如果我的理解是正确的,这个答案怎么样?请将此视为几个可能的答案之一。

修改点:

  • 在这种情况下,我想提议为 multipart/ralated 创建包含文件和元数据的结构并提出请求。

修改后的脚本:

当你的脚本修改后,变成如下。

public function uploadByCurl($uploadFilePath, $accessToken){
    $handle = fopen($uploadFilePath, "rb");
    $file = fread($handle, filesize($uploadFilePath));
    fclose($handle);

    $boundary = "xxxxxxxxxx";
    $data = "--" . $boundary . "\r\n";
    $data .= "Content-Type: application/json; charset=UTF-8\r\n\r\n";
    $data .= "{\"name\": \"" . basename($uploadFilePath) . "\", \"mimeType\": \"" . mime_content_type($uploadFilePath) . "\"}\r\n";
    $data .= "--" . $boundary . "\r\n";
    $data .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $data .= base64_encode($file);
    $data .= "\r\n--" . $boundary . "--";

    $ch = curl_init();
    $options = [
        CURLOPT_URL =>  'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_HTTPHEADER => [
            'Authorization:Bearer ' . $accessToken,
            'Content-Type:multipart/related; boundary=' . $boundary,
        ],
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => 0,
    ];
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    return $result;
}
  • 在这个修改后的脚本中,文件名和 mimeType 是从 $uploadFilePath 中检索的。

注:

  • 分段上传可以上传小于 5 MB 的文件。请注意这一点。

参考文献:

  • Perform a multipart upload

    Multipart upload: uploadType=multipart. For quick transfer of a small file (5 MB or less) and metadata that describes the file, all in a single request.

如果我误解了你的问题,这不是你想要的方向,我很抱歉。