使用PHP和cURL通过Google电子表格API创建XLS电子表格
问题描述:
我试图使用PHP和cURL从Google Drive中的XLS创建电子表格。我成功创建了.doc文档,我可以从.csv创建电子表格,但是当我尝试从xls文件创建文档时,它只是以“文件”的形式保存在Google Drives中,无需预览,并且只能下载它。使用PHP和cURL通过Google电子表格API创建XLS电子表格
我用下面的代码来启动可恢复上传:
$curl = curl_init();
$upload_href = 'https://docs.google.com/feeds/upload/create-session/default/private/full'; // I'm actually taking the upload href from document listing
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$filename = 'MyFile';
$type = 'spreadsheet';
$content_type = 'application/msexcel'; // I can use 'text/csv' here to upload csv files just alright
$body = '<?xml version="1.0" encoding="UTF-8"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007"><category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/docs/2007#'.$type.'"/><title>'.$filename.'</title></entry>';
$headers = array(
"Authorization: GoogleLogin auth=" . $g_auth, // using ClientLogin
"GData-Version: 3.0",
"Content-Length: ".strlen($body),
"Content-Type: application/atom+xml",
"X-Upload-Content-Type: ".$content_type,
"X-Upload-Content-Length: ".$buffer_size // the size of the spreadsheet to be uploaded
);
curl_setopt($curl, CURLOPT_URL, $upload_href);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl);
// ...
// what follows is the start of resumable upload
谷歌电子表格API点,谷歌文档API如何创建一个电子表格,但谷歌文档API没有指向具体的事情关于创建电子表格。
我想我在这里所犯的错误是错误的ContentType,但我不知道在哪里查找Google API接受的内容类型的有效列表。
答
您是否尝试过MIME类型为application/vnd.google-apps.spreadsheet
?这是Google API可以接受的MIME类型。
这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 – netcoder
@netcoder请阅读Ethnar最近的声明,他/她说他/他不知道在哪里查找Google API接受的有效内容类型列表。我只是提供一个。如果这不是一个有用的答案,那么请坚持我删除它。谢谢 – Daydah
谢谢你的回答。我尝试使用mime application/vnd.google-apps.spreadsheet,结果很奇怪。该文件上传到谷歌文档,但我收到消息“该网址的电子表格无法找到。”。当浏览谷歌驱动器上的文件时,我可以看到列表中的文件(如电子表格,这是一些成功),试图查看它,我最终以相同的消息(未找到)。我会检查以确保所有请求都正确地通过,Google文档确认文件上传成功。这个哑剧肯定让我感觉到某个地方,再次感谢。 – Ethnar