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
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;
}
I just don't know how to translate this to code(not familiar with multipart/related
):
- Metadata part. Must come first, and must have a Content-Type header set to application/json; charset=UTF-8. Add the file's metadata to this part in JSON format.
- Media part. Must come second, and must have a Content-Type header, which may have any MIME type. Add the file's data to this part.