How do I read a Google Drive Spreadsheet in PHP?

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 File -> Publish to the Web
  3. In the Publish to the web dialog you can get a .csv Link to your spreadsheet.

enter image description here

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");

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)