簡體   English   中英

Google Docs API。 使用curl上傳文件

[英]Google Docs API. Upload file using curl

任何人都知道如何使用curl將文件發布到谷歌文檔。 我寫這段代碼:

$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);    

並且只有當我使用普通/文本Content-type和文本文件時它才能正常工作,但如果我在二進制模式下上傳的文件我得到了417 http錯誤代碼,例如pdf文檔。

我試着改變這一行

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

對此

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

但我又得到了417響應錯誤代碼。 只有普通/文本響應是201。

來自官方來源的Google文檔上傳標題示例

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 ...

看看這篇文章 它提供了關於如何使用CURL的精彩文章,包括如何發布變量。 我不確定如何使用谷歌文檔,但只要你按照他們的指示,這應該可以幫助你找出你的post var問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM