使用 PHP 和 cURL 通过 Google 电子表格 API 从 XLS 创建电子表格


Creating spreadsheet from XLS through Google spreadsheet API with PHP and cURL

我正在尝试使用PHP和cURL从Google云端硬盘中的XLS创建电子表格。我成功地从.doc创建了文档,我可以从.csv创建电子表格,但是当我尝试从xls文件创建文档时,它只是作为"文件"保存在Google云端硬盘中,没有预览,可以选择仅下载它。

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

$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 并未指出有关创建电子表格的任何具体内容。

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

您是否尝试过 mime 类型作为application/vnd.google-apps.spreadsheet?它是谷歌API可接受的MIME类型