PHP google drive api创建电子表格

时间:2012-08-16 14:52:53

标签: php google-drive-api

我需要使用google驱动PHP API创建电子表格,我该怎么做? 我已经有代码把它总是得到无标题文件,当我点击它时,这里没有数据是我的代码:

/**
* Insert new file.
*
* @param apiDriveService $service Drive API service instance.
* @param string $title Title of the file to insert, including the extension.
* @param string $description Description of the file to insert.
* @param string $parentId Parent folder's ID.
* @param string $mimeType MIME type of the file to insert.
* @param string $filename Filename of the file to insert.
* @return DriveFile The file that was inserted. NULL is returned if an API error occurred.
*/
function insertFile($service, $title, $description, $parentId, $mimeType, $data) {
$file = new DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);

// Set the parent folder.
if ($parentId != null) {
    $parentsCollectionData = new DriveFileParentsCollection();
    $parentsCollectionData->setId($parentId);
    $file->setParentsCollection(array($parentsCollectionData));
}

try {

    $createdFile = $service->files->insert($file, array(
        'data' => $data,
        'mimeType' => $mimeType,
    ));

    // Uncomment the following line to print the File ID
    // print 'File ID: %s' % $createdFile->getId();

    print_r($createdFile);
} catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}
}
if(isset($_POST['insertFile'])){
$service = new apiDriveService($client);
insertFile($service,'Google Maps Mrakers.txt','ADD Google Map Mrakers To File',NULL,'text/plain',$_POST['data']);
exit;
}

1 个答案:

答案 0 :(得分:4)

您遇到此问题是因为您使用的是PHP客户端库的过时版本。请将您的客户端同步到项目的主干repository

您必须重新编写一些代码,因为自您第一次编写代码以来客户端库已被重构。 reference guides中的Google Drive SDK documentation已更新,以反映此重构。

要创建Google电子表格,请插入一个没有内容且其mimeType设置为application/vnd.google-apps.spreadsheet的文件。