【问题标题】:Fetching Google Docs Spreadsheet Data using PHP使用 PHP 获取 Google Docs 电子表格数据
【发布时间】:2011-08-15 08:36:51
【问题描述】:

我正在尝试从 google docs 电子表格中获取数据,我认为这应该可以工作(在此处的另一个类似问题中也看到了类似的代码),但我得到的回复是:

Moved Temporarily The document has moved here.here 的链接是:

https://www.google.com/a/4playtheband.co.uk/ServiceLogin?service=wise&continue=https://docs.google.com/a/4playtheband.co.uk/spreadsheet/pub?hl%3Den_US%26hl%3Den_US%26key%3D0AuJb1YSvmVn5dGdvUzU2QUJHUGdaTEZNbVI4dVJ6eHc%26single%3Dtrue%26gid%3D0%26output%3Dcsv%26ndplr%3D1&followup=https://docs.google.com/a/4playtheband.co.uk/spreadsheet/pub?hl%3Den_US%26hl%3Den_US%26key%3D0AuJb1YSvmVn5dGdvUzU2QUJHUGdaTEZNbVI4dVJ6eHc%26single%3Dtrue%26gid%3D0%26output%3Dcsv%26ndplr%3D1&hl=en_US&passive=true&go=true

我不知道问题是否与在 Google Apps 中使用的相关文档有关,但我已将电子表格设置为公开,并复制了它在访问 Share 时提供的链接并将其导出作为 csv。

代码如下:

<?php

$url='https://docs.google.com/a/4playtheband.co.uk/spreadsheet/pub?hl=en_US&hl=en_US&key=0AuJb1YSvmVn5dGdvUzU2QUJHUGdaTEZNbVI4dVJ6eHc&single=true&gid=0&output=csv';

if (($handle = fopen($url, "r")) !== FALSE)
{
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
        $totalrows = count($data);
        for ($row=0; $row<=$totalrows; $row++)
        {
            echo $data[$row];
        }
    }
    fclose($handle);
}
?>

如果这可能是一个问题,我实际上是在尝试将获取的数据放在一个 Facebook 中,它似乎可以正常工作,除了实际的电子表格数据没有出现,因为我之前提到的错误消息实际上出现在了该 Facebook 中。

谢谢。

更新 当电子表格是在我的个人 Google 帐户而不是我用于 Google Apps 的帐户中创建时,代码似乎可以正常工作。

【问题讨论】:

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


    【解决方案1】:

    您将获得 HTTP 302 状态代码,这意味着您应该点击指向新 URL 的链接。

    【讨论】:

    • 谢谢,但我确定我尝试使用该新网址,但它只返回相同的消息。我刚刚又试了一次,可以确认没有任何变化:(
    【解决方案2】:

    我认为这是因为 URL 通过发出 302 重定向而 fopen 无法遵循 HTTP 重定向。我认为使用 curl 绑定可以解决这个问题。

    还可以考虑将 Zend GData 库用作 Google Docs 的高级 API。 http://framework.zend.com/manual/en/zend.gdata.spreadsheets.html

    【讨论】:

    • 谢谢,但不幸的是我没有使用 Zend。我会看看你的建议,谢谢。
    【解决方案3】:

    我首先在我自己的谷歌帐户而不是谷歌应用程序中使用电子表格,然后使用以下代码解决了这个问题:

    <div id="faqSection">
    <!-- The FAQs are inserted here -->
    <p>
    
    <?php
    // link to Google Docs spreadsheet
    $url='google docs spreadsheet url';
    
    // open file for reading
    if (($handle = fopen($url, "r")) !== FALSE)
    {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
        {
            $totalrows = count($data);
            for ($row=0; $row<=$totalrows; $row++)
            {
                // only print out even rows, i.e. the questions
                // the second if is to avoid blank divs from being printed out
                // which was a bug I was experiencing
                if (($row % 2 == 0) && (strlen($data[$row])>0))
                {
                    $answer = $row + 1;
                    echo '<dt><span class="icon"></span>'.$data[$row].'</dt><dd>'.$data[$answer].'</dd>';
                }
            }
        }
        fclose($handle);
    }
    ?>
    </p>
    </div>
    

    【讨论】:

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