I’m trying to create a Spreadsheet from XLS in Google Drive using PHP and cURL. I successfully created Document from .doc, I can create spreadsheet from .csv, but when I’m trying to create a document from xls file, it’s just saved in Google Drives as “File”, without preview and with option to only download it.
I’m using the following code to initiate the resumable upload:
$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 points to Google Docs API on how to create a spreadsheet, but the Google Docs API doesn’t point anything specific regarding creating spreadsheets.
I guess the mistake I’m making here is the wrong ContentType, but I have no idea where to look for a valid list of content types Google API accepts.
Have you tried mime type as
application/vnd.google-apps.spreadsheet? It is an acceptable mime type by Google API