【问题标题】:Google Docs API. Upload file using curl谷歌文档 API。使用 curl 上传文件
【发布时间】:2010-02-15 18:08:08
【问题描述】:

任何人都知道如何使用 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);    

只有当我对文本文件使用纯/文本内容类型时,它才能正常工作,但如果我以二进制模式上传文件,我会收到 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。

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

【问题讨论】:

    标签: php curl file-upload google-docs google-docs-api


    【解决方案1】:

    查看this article。它提供了关于如何使用 CURL 的精彩文章,包括如何发布变量。我不确定如何将其与 google docs 一起使用,但只要您按照他们的说明进行操作,那么这应该可以帮助您找出您的 post var 问题。

    【讨论】:

    • 我之前看过这篇文章,它只展示了如何发布变量而不是文件:(
    • 啊,试试这个链接——它是 CURL 手册页的一部分:(cs.sunysb.edu/documentation/curl/index.html) 在标题“POST (HTTP)”下查看他们讨论移动文件 'cooltext.txt ' 这可能会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多