【问题标题】:Create empty spreadsheet in Google Docs via cURL and PHP通过 cURL 和 PHP 在 Google Docs 中创建空电子表格
【发布时间】:2012-07-04 10:08:24
【问题描述】:

我正在尝试在不使用 Zend 库的情况下在 Google 文档中创建一个新的电子表格。我浏览了文档并尝试按照步骤操作,但现在我被卡住了。我的代码是这样的

    $curl = curl_init();

    $headers = array(
        "GData-Version: 3.0",
        "Authorization: GoogleLogin auth=" . $myToken,
        "Content-Length: 350",
        "Content-Type: application/atom+xml",
        "X-Upload-Content-Length: 0"
    );      

    $post_field = '<?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#spreadsheet"/>
        <title>Mytitle</title>
        </entry>';

    curl_setopt($curl, CURLOPT_URL, 'https://docs.google.com/feeds/upload/create-session/default/private/full');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_field);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);      

    $response = curl_exec($curl);
    curl_close($curl);

令牌似乎工作正常,因为我可以使用它从谷歌文档中检索数据。令我惊讶的是,我没有得到 API 的响应。在尝试不同的解决方案时,我通常会收到一条错误消息,但这里的响应字符串是空的。

【问题讨论】:

  • 您收到的 HTTP 响应代码是什么? (var_dump(curl_getinfo($curl, CURLINFO_HTTP_CODE));)
  • 我得到一个状态码200,所以请求成功了。我正在寻找的请求代码是 201

标签: php curl google-docs-api google-drive-api


【解决方案1】:

您正在发送请求以启动可恢复的上传会话 (https://developers.google.com/gdata/docs/resumable_upload),但从未发送实际内容。

创建空文件时不需要恢复上传,请将您的请求发送到https://docs.google.com/feeds/default/private/full

【讨论】:

    【解决方案2】:

    如果可以,您应该改用 Google Drive API 的 files.insert 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多