如何通过使用Guzzle将文件以“多部分”类型上传到GoogleDrive?

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

借助@Tanaike(请参阅here),我可以使用php-curl成功将文件上传到GoogleDrive,这是我使用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客户端)执行相同的操作时,文件(图像文件)可以成功上传,但是其名称为“无标题”,并且我无法预览图片(请参见下面的屏幕截图):

enter image description here

enter image description here

这是我使用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);
google-drive-api guzzle php-curl
1个回答
1
投票

我认为请求正文是正确的。在您的脚本中,Content-Type不正确。在这种情况下,我认为body作为application/octet-stream发送。由此,创建了Untitled的文件。我认为这是请求正文的文本数据。

那么修改如何?

发件人:

'Content-Type' => 'Content-Type: multipart/related; boundary=' . $boundary,

收件人:

'Content-Type' => 'multipart/related; boundary=' . $boundary,
© www.soinside.com 2019 - 2024. All rights reserved.