开发者

Fetching Google Docs Spreadsheet Data using PHP

开发者 https://www.devze.com 2023-03-28 16:41 出处:网络
I am trying to fetch the data from a google docs spreadsheet and I think this should work (also seen similar code in another similar question on here) but the response I get is:

I am trying to fetch the data from a google docs spreadsheet and I think this should work (also seen similar code in another similar question on here) but the response I get is:

Moved Temporarily The document has moved here. with the link to here being:

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

I开发者_如何学编程 don't know if the problem is to do with the docs in question being used inside Google Apps but I have set the spreadsheet to be public and I copied the link it provided when I went to Share and exported it as a csv.

Here is the code:

<?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);
}
?>

If it might be an issue I am actually trying to place the fetched data inside a facebox and it seems to be working except for the actual spreadsheet data not appearing since the error message I mentioned earlier actually appears inside the facebox.

Thanks.

UPDATE The code seems to work perfectly fine when the spreadsheet is created within my personal google account and not the account that I use for Google Apps.


You are getting a HTTP 302 status code, which means you should follow the link to the new URL.


I think its because the URL redirects by issuing a 302 and fopen can't follow HTTP redirects. I would think using curl bindings would resolve this issue.

Also consider using the Zend GData library as a high level API for Google Docs. http://framework.zend.com/manual/en/zend.gdata.spreadsheets.html


I managed to fix this by firstly using a spreadsheet inside my own google account and not google apps and by then using the following code:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号