【问题标题】:Creating spreadsheet from XLS through Google spreadsheet API with PHP and cURL使用 PHP 和 cURL 通过 Google 电子表格 API 从 XLS 创建电子表格
【发布时间】:2012-06-18 19:31:43
【问题描述】:

我正在尝试使用 PHP 和 cURL 从 Google Drive 中的 XLS 创建一个电子表格。我从 .doc 成功创建了文档,我可以从 .csv 创建电子表格,但是当我尝试从 xls 文件创建文档时,它只是作为“文件”保存在 Google Drives 中,没有预览并且可以选择仅下载它.

我正在使用以下代码来启动可恢复上传:

$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

Google Spreadsheet API 指向 Google Docs API 关于如何创建电子表格,但 Google Docs API 没有指出任何关于创建电子表格的具体内容。

我想我在这里犯的错误是错误的 ContentType,但我不知道在哪里可以找到 Google API 接受的内容类型的有效列表。

【问题讨论】:

    标签: php curl google-spreadsheet-api


    【解决方案1】:

    您是否尝试过将 MIME 类型设为 application/vnd.google-apps.spreadsheet?它是 Google API 可接受的 MIME 类型

    【讨论】:

    • 这没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
    • @netcoder 请阅读 Ethnar 的最后声明,他/她说她/他不知道在哪里可以找到 Google API 接受的有效内容类型列表。我只是提供了一个。如果这不是一个有用的答案,那么请坚持我将其删除。谢谢
    • 感谢您的回答。我尝试使用 mime application/vnd.google-apps.spreadsheet,结果很奇怪。该文件已上传到谷歌文档,但我收到消息“找不到此 URL 的电子表格。”。当浏览谷歌驱动器上的文件时,我可以在列表中看到文件(作为电子表格,所以这是一些成功的),试图查看它我最终得到相同的消息(未找到)。我会检查以确保所有请求都正常通过,并且谷歌文档确认文件上传成功。这个哑剧肯定能把我带到某个地方,再次感谢。
    • @Ethnar 很高兴能提供帮助。请分享您的进步,以便我们都可以了解以后不要犯的错误。
    • 所有上传都正常进行,没有遇到任何错误,所有回复代码都表示每个阶段都成功。我发现“无法找到此 URL 的电子表格”错误对于甚至不使用 API 的人来说很常见。我发现了一些提示,表明谷歌文档中也有每个工作表的权限,并且对于某些人来说,它在与他人共享工作时会导致问题。我现在不能再花时间调查这个问题,但我一定会稍后检查它,如果我碰巧找到任何解决方案,我一定会写在这里。再次感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多