如何以“多部分"方式将文件上传到GoogleDrive通过使用php-curl键入? [英] How can I upload files to GoogleDrive in "multipart" type by using php-curl?
问题描述
除了 google-api-php-client之外,我想使用php curl上传,但我真的不知道该怎么做,这里是文档:发送分段上传请求
I want to upload in using php curl other than 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
中,有人可以帮助我吗?
Here is my code snippet, I stuck in CURLOPT_POSTFIELDS
, can anybody help me with this?
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
):
I just don't know how to translate this to code(not familiar with multipart/related
):
- 元数据部分.必须排在最前面,并且必须将Content-Type标头设置为application/json; charset = UTF-8.将文件的元数据以JSON格式添加到此部分.
- 媒体部分.必须排在第二位,并且必须具有Content-Type标头,该标头可以具有任何MIME类型.将文件的数据添加到此部分.
推荐答案
- 您要使用带有驱动器API v3的
multipart/ralated
上传文件. - 您想使用PHP CURL实现这一目标.
- 您的访问令牌可用于将文件上传到Google云端硬盘.
- You want to upload a file using
multipart/ralated
with Drive API v3. - You want to achieve this using PHP CURL.
- Your access token can be used for uploading the file to Google Drive.
- 在这种情况下,我想建议创建一个结构,包括
multipart/ralated
的文件和元数据,并请求它. - In this case, I would like to propose to create the structure including the file and the metadata for
multipart/ralated
and request it.
如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
修改脚本后,它如下所示.
When your script is modified, it becomes as follows.
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;
}
- 在此修改后的脚本中,从
$uploadFilePath
中检索文件名和mimeType. - At this modified script, the filename and mimeType are retrieved from
$uploadFilePath
. - 分段上传可以上传小于5 MB的文件.请注意这一点.
-
执行分段上传 >
分段上传:uploadType = multipart.快速传输小文件(5 MB或更少)和描述文件的元数据,全部都在一个请求中.
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.
如果我误解了你的问题,而这不是你想要的方向,我深表歉意.
If I misunderstood your question and this was not the direction you want, I apologize.
这篇关于如何以“多部分"方式将文件上传到GoogleDrive通过使用php-curl键入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!