使用带有谷歌驱动器的可恢复上传

时间:2016-06-08 08:50:20

标签: php curl google-drive-api

我试图通过Google云端硬盘使用此文档在PHP中进行可恢复上传:https://developers.google.com/drive/v3/web/manage-uploads#resumable

我需要在没有sdk的情况下完成。

我已经管理了一个简单的上传,但是当我尝试开始一个可恢复的会话时,我的cURL不会返回任何内容。

这是我的cURL的样子:

$curl_header = array(
    'Authorization: Bearer '.$this->_token,
    'X-Upload-Content-Length: '.$total_file_size,
    'X-Upload-Content-Type: '.$mime_type
);

$datas = array();

$url_upload = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable';

$curl_default_options = array(
    // Default option (http://php.net/manual/fr/function.curl-setopt.php)
    CURLOPT_HTTPHEADER => $curl_header,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_SSL_VERIFYPEER => true,
    CURLOPT_CAINFO => $path_cert.'/googledrive/certificat.pem',
    CURLOPT_URL        => $url_upload,
    CURLOPT_POST       => true,
    CURLOPT_POSTFIELDS => $datas
);

$curl = curl_init();

curl_setopt_array($curl, $curl_default_options);

if (count($curl_more_options)) {
    curl_setopt_array($curl, $curl_more_options);
}

我做了一个" curl_getinfo($ curl)"得到了:

Array ( 
    [url] => https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable 
    [content_type] => text/html; charset=UTF-8 
    [http_code] => 200 
    [header_size] => 699 
    [request_size] => 288 
    [filetime] => -1 
    [ssl_verify_result] => 0 
    [redirect_count] => 0 
    [total_time] => 0.203 
    [namelookup_time] => 0 
    [connect_time] => 0.016 
    [pretransfer_time] => 0.047 
    [size_upload] => 0 
    [size_download] => 0 
    [speed_download] => 0 
    [speed_upload] => 0 
    [download_content_length] => 0 
    [upload_content_length] => -1 
    [starttransfer_time] => 0.203 
    [redirect_time] => 0 
    [redirect_url] => 
    [primary_ip] => 216.58.208.202 
    [certinfo] => Array ( ) 
    [primary_port] => 443 
    [local_ip] => 192.168.0.44 
    [local_port] => 56440 
)

1 个答案:

答案 0 :(得分:0)

已经很长时间了,但一定是这样的:

  • 第一部分上传:

    $curl_headers = array(
        'Content-Type: application/json',
        'X-Upload-Content-Length: '.$total_file_size,
        'X-Upload-Content-Type: '.$mime_type
    );
    
    $datas = '{
        "name": "'.$name.'",
        "parents": ["'.$id_file_destination.'"]
     }';
    
    $url = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable';
    
    $curl_default_options = array(
        CURLOPT_HTTPHEADER     => $curl_header,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_SSL_VERIFYPEER => true,
        CURLOPT_CAINFO         => $path_cert.'/certificat.pem',
        CURLOPT_URL            => $url,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $datas,
        CURLOPT_HEADER         => true
    );
    

您将收到一个会话,用于恢复上传。

  • 断点续传:

    $header = array(
        'Content-Length: '.$content_length,
        'Content-Type: '.$mime_type,
        'Content-Range: bytes '.$byte_start.'-'.$byte_end.'/'.$total_file_size,
    );
    
    $datas = fread($file, $content_length);
    
    $url = $session;
    
    $curl_default_options = array(
        CURLOPT_HTTPHEADER     => $curl_header,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_SSL_VERIFYPEER => true,
        CURLOPT_CAINFO         => $path_cert.'/certificat.pem',
        CURLOPT_URL            => $url,
        CURLOPT_CUSTOMREQUEST  => 'PUT',
        CURLOPT_INFILE         => $file,
        CURLOPT_INFILESIZE     => $total_file_size,
        CURLOPT_POSTFIELDS     => $datas,
        CURLOPT_HEADER         => true,
    );