如何使用Guzzle将文件以“多部分"类型上传到GoogleDrive? [英] How can I upload files to GoogleDrive in “multipart” type by using Guzzle?
问题描述
With the help of @Tanaike(see here), I can successfully upload files to GoogleDrive by using php-curl, here is my code by using php-curl:
function uploadByCurl($uploadFilePath, $accessToken){
$ch = curl_init();
$boundary = md5(mt_rand() . microtime());
//MetaData
$params = json_encode([
'name' => basename($uploadFilePath),
'description' => 'This is a test',
]);
$fileContent = file_get_contents($uploadFilePath);
$dataToUpload = "--{$boundary}\r\n";
$dataToUpload .= "Content-Type: application/json\r\n\r\n";
$dataToUpload .= "{$params}\r\n";
$dataToUpload .= "--{$boundary}\r\n";
$dataToUpload .= "Content-Transfer-Encoding: base64\r\n\r\n";
$dataToUpload .= base64_encode($fileContent) . "\r\n";
$dataToUpload .= "--{$boundary}--";
$options = [
CURLOPT_URL => 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $dataToUpload,
CURLOPT_HTTPHEADER => [
'Authorization:Bearer ' . $accessToken,
'Content-Type: multipart/related; boundary=' . $boundary,
],
//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;
}
$uploadFilePath = '<YOU-FILE-ABS-PATH>';
$accessToken = '<YOU-ACCESS-TOKEN>';
$ret = uploadByCurl($uploadFilePath, $accessToken);
var_dump($ret);
响应json:
{
"kind": "drive#file",
"id": "1lkuCTeMooRmde8uzNa7COUTTCLAk_jdq",
"name": "timg (12).jpeg",
"mimeType": "image/jpeg"
}
但是当我尝试使用 Guzzle6 (用PHP编写的curl客户端)执行相同的操作,文件(图像文件)可以成功上传,但名称为无标题",并且我无法预览图像(请参见下面的屏幕截图):
But when I'm trying to use Guzzle6(An curl client written by PHP) to do the same thing, the file(image file) can successfully upload but its name is "Untitled" and I can't preview the image(see the screenshot below):
这是我使用Guzzle的代码段:
Here is my code snippet by using Guzzle:
function uploadByGuzzle($uploadFilePath, $accessToken){
$boundary = md5(mt_rand() . microtime());
//MetaData
$params = json_encode([
'name' => basename($uploadFilePath),
'description' => 'This is a test',
]);
$fileContent = file_get_contents($uploadFilePath);
$dataToUpload = "--{$boundary}\r\n";
$dataToUpload .= "Content-Type: application/json\r\n\r\n";
$dataToUpload .= "{$params}\r\n";
$dataToUpload .= "--{$boundary}\r\n";
$dataToUpload .= "Content-Transfer-Encoding: base64\r\n\r\n";
$dataToUpload .= base64_encode($fileContent) . "\r\n";
$dataToUpload .= "--{$boundary}--";
$GuzzleConfig = [
'base_uri' => 'https://www.googleapis.com/upload/drive/v3/files/',
'timeout' => 30.0,
];
//In case you need a proxy
$GuzzleConfig['proxy'] = 'http://127.0.0.1:1087';
//GuzzleHttp
$client = new Client($GuzzleConfig);
$uri = '?uploadType=multipart';
$response = $client->request('POST', $uri, [
'verify' => false,
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
'Content-Type' => 'Content-Type: multipart/related; boundary=' . $boundary,
],
'body' => $dataToUpload,
]);
$string = $response->getBody()->getContents();
return $string;
}
$uploadFilePath = '<YOU-FILE-ABS-PATH>';
$accessToken = '<YOU-ACCESS-TOKEN>';
$ret = uploadByGuzzle($uploadFilePath, $accessToken);
var_dump($ret);
推荐答案
我认为请求正文是正确的.在您的脚本中,Content-Type
是不正确的.在这种情况下,我认为body
作为application/octet-stream
发送.这样,创建了Untitled
的文件.我认为这是请求正文的文本数据.
I think that the request body is correct. In your script, Content-Type
is not correct. In this case, I think that body
is sent as application/octet-stream
. By this, the file of Untitled
is created. And I think that it is the text data of the request body.
那修改呢?
'Content-Type' => 'Content-Type: multipart/related; boundary=' . $boundary,
收件人:
'Content-Type' => 'multipart/related; boundary=' . $boundary,
这篇关于如何使用Guzzle将文件以“多部分"类型上传到GoogleDrive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!