使用google drive php api上传了空内容的文件

时间:2014-06-20 07:48:21

标签: php google-api google-api-php-client google-api-client google-drive-api

以下是代码。使用google drive php api成功上传/创建/插入google驱动器上的文件,但上传文件后其内容为空。

session_start();
set_include_path("/src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';

$client_id = 'XXXXXX';
$client_secret = 'XXXXXX';
$redirect_uri = 'XXXXXX';

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");

$service = new Google_Service_Drive($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['upload_token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['upload_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
  $client->setAccessToken($_SESSION['upload_token']);
  if ($client->isAccessTokenExpired()) {
    unset($_SESSION['upload_token']);
  }
} else {
  $authUrl = $client->createAuthUrl();
}

$filename = "file.csv";

echo "<BR><BR>stage 1";

if ($client->getAccessToken())
{       
    $data = file_get_contents($filename);

    if (isset($data) && $data)
    {
        $file = new Google_Service_Drive_DriveFile();
        echo "<BR><BR>stage 2";
        if (isset($file))
        {            
            $file -> setTitle("gsexpo");
            $file -> setMimeType("application/vnd.google-apps.spreadsheet");

            if (isset($file -> parentId) && $file -> parentId != null) {
                $parent = new Google_ParentReference();
                $parent -> setId($file -> parentId);
                $file -> setParents(array($parent));
            }

            echo "<BR><BR>stage 3";

            $result2 = $service->files->insert(
                $file,
                array(
                  'data' => $data,
                  'mimeType'   => "application/vnd.google-apps.spreadsheet",                  
                  'convert' => true
                ));

            echo "Result: ";            
            var_dump($result2->title);
        }
        else
        {
            echo "No file object";
        }
    }
    else
    {
        echo "File read failed";    
    }
}
if (isset($authUrl)) 
{     echo "<a href='".$authUrl."'>Connect Me!</a>";    }

这是我用于此目的的google api php客户端库的链接。

https://github.com/google/google-api-php-client

2 个答案:

答案 0 :(得分:0)

您需要将“uploadType”参数添加到插入请求数组中。例如:

$result2 = $service->files->insert(
    $file,
    array(
         'data' => $data,
         'mimeType'   => "application/vnd.google-apps.spreadsheet",                  
         'convert' => true,
         'uploadType' => 'media',
    )
);

答案 1 :(得分:0)

只需使用以下代码:

$result2 = $service->files->create($file, array(
            'data' => $data,
            'mimeType' => 'application/vnd.google-apps.spreadsheet',
            'uploadType' => 'multipart'
        ));

如果有任何问题,请告诉我。