Question

Anyone know how to post file using curl to the google documents. I write this code:

$header[] = "Authorization: GoogleLogin auth=seсretkey";
$header[] = "Content-Type: application/pdf";
$header[] = "Slug: testdoc";
$header[] = "Content-Length: ".$_FILES['file']['size']."";
$url = "http://docs.google.com/feeds/documents/private/full";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);            
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($_FILES['file']['tmp_name']));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);    

And it works fine only if i use plain/text Content-type with text files, but if my uploaded file in binary mode i got 417 http error code, for example with pdf documents.

I've trying change this line

curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($_FILES['file']['tmp_name']));

To this

curl_setopt($ch, CURLOPT_POSTFIELDS, array("file"=>"@".$_FILES['file']['tmp_name']));

But i've got 417 response error code again. Only with plain/text response is 201.

Example of the google documents upload headers from official source

POST /feeds/documents/private/full HTTP/1.1
Host: docs.google.com
Authorization: <your authorization header here>
Content-Length: 81047
Content-Type: application/vnd.ms-excel
Slug: Example Spreadsheet

... spreadsheet contents here ...
Was it helpful?

Solution

Check out this article. It gives a great writeup on how to use CURL including how to post variables. I am not sure on how to use this with google docs, but as long as you are following their instructions then this should help you figure out your post var issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top