Post CSV to Google Docs Folder
Working on a PHP script to essentially dump the results of a query into a .csv and then upload to a specific folder on our enterprise Google Docs.
The file is created and sent over to Docs just fine. However, it isn't being placed in the proper directory.
This is the function I'm using where $file
, $folder
, and $newtoken
are strings like 'dump.csv'
, '0B0rVKeOmIwGXMGU2MzYyN2EtZWV...'
, [auth token from Google]
.
function insert($file, $folder, $newtoken){
$mime = "Media multipart posting\n--END_OF_PART\n";
$mime .= "Content-Type: application/atom+xml\n\n";
$xml="<?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#document\"/>
<title>example document</title>
<docs:writersCanInvite value=\"true\" />
</entry>
";
$mime.=$xml."\n--END_OF_PART\n";
$document = "Content-Type: text/plain\n\n";
$handle = fopen($file, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$document.=$contents;
$itemURL = "https://docs.google.com/feeds/default/private/full";
$mime .= $document."\n--END_OF_PART--\n";
$headers = array();
$headers[] = "POST /feeds/default/private/full/folder%3A".$folder."/contents HTTP/1.1";
$headers[] = "Host: docs.google.com";
$headers[] = "GData-Version: 3.0";
$headers[] = "Aut开发者_JAVA百科horization: GoogleLogin auth=".$newtoken."";
$headers[] = "Content-Length: ".strlen($mime);
$headers[] = "Content-Type: multipart/related; boundary=END_OF_PART";
$headers[] = "Slug: Test";
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$itemURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl,CURLOPT_POSTFIELDS, $mime);
$result = curl_exec($curl);
curl_close($curl);
echo 'Response: '.$result;
}
Maybe I have the folder ID wrong (just copied from URL while viewing in Google Docs)?
Bonus: Can I manipulate the content-type headers to get Google to convert the file to a spreadsheet?You have
$headers[] = "POST /feeds/default/private/full/folder%3A".$folder."/contents HTTP/1.1";
and then:
curl_setopt($curl,CURLOPT_URL,$itemURL);
Make up your mind. I think the first one is wrong; you can't change the URL via the CURLOPT_HTTPHEADER
option. Set $itemURL
to the full URL (including /feeds/default/private/full/folder%3A".$folder."/contents
) and remove the first line.
精彩评论