How do I read a Google Drive Spreadsheet in PHP?

后端 未结 5 1488
失恋的感觉
失恋的感觉 2020-12-07 12:06

All I\'m trying to do is read a Google Spreadsheet from a web site. I\'ve read and re-read the Google Drive API docs and everything Google Drive PHP on Stack Overflow and I

相关标签:
5条回答
  • 2020-12-07 12:46

    You need to use oauth, if you don't google will only allow you to make a few requests.

    If all you want to do is read data out of a google spreadsheet or write data into it then you can just use the spreadsheet api. Check out php-google-spreadsheet-client.

    0 讨论(0)
  • 2020-12-07 12:55

    There is also a much easier, but less clean solution, if you don't want to bother with the API or Google Authentication.

    1. Go to your Spreadsheet in Google Drive.
    2. Go to files -> Publish on the Web
    3. Select the right worksheet and make sure to enable the checkbox always update.
    4. Still in the same box you can get a .csv Link to your spreadsheet.

    You can now access the contents like any other csv File on the Web. Here is some sample code:

    $spreadsheet_url="https://docs.google.com/spreadsheet/pub?key=<somecode>&single=true&gid=0&output=csv";
    
    if(!ini_set('default_socket_timeout', 15)) echo "<!-- unable to change socket timeout -->";
    
    if (($handle = fopen($spreadsheet_url, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $spreadsheet_data[] = $data;
        }
        fclose($handle);
    }
    else
        die("Problem reading csv");
    
    0 讨论(0)
  • 2020-12-07 13:00

    If you want you own file to be read you need a service account instead of a "Client ID for web applications". I've been battling this problem myself for way to long and this brought me the sollution: https://developers.google.com/drive/web/service-accounts

    0 讨论(0)
  • 2020-12-07 13:01

    I created a sample project that uses a service account to authenticate against Google Spreadsheets in order to access to the contents of a spreadsheet.

    Have a look at the README at https://github.com/juampynr/google-spreadsheet-reader.

    0 讨论(0)
  • 2020-12-07 13:11

    Please check the Google Drive PHP Quickstart. You have not actually authorized your client. Starting from $authUrl = $client->createAuthUrl();

    All Google Drive requests need authorization of some kind.

    0 讨论(0)
提交回复
热议问题