How to get data from Google Spreadsheet to your PHP application?

There are several ways to get API data to your PHP application. If you use the composer in your project, we recommend using SheetDB PHP library or Guzzle. If not you can use vanilla PHP functions like file_get_contents to get the job done. Let's check both cases.

SheetDB PHP Library

The description how to use it is on the GitHub page, but in short. First you need to install library, type in your terminal:

composer require sheetdb/sheetdb-php

Than import it and use like in GitHub page. Here are some examples:

<?php require('vendor/autoload.php'); use SheetDB\SheetDB; $sheetdb = new SheetDB('58f61be4dda40'); // returns all spreadsheets data $response = $sheetdb->get(); // returns when name="Steve" AND age=22 $response = $sheetdb->search(['name'=>'Steve','age'=>'22']); // returns when name="Steve" OR age=19 $response = $sheetdb->searchOr(['name'=>'Steve','age'=>'19']); // insert one row $sheetdb->create(['name'=>'Mark','age'=>'35']); // insert array of rows $sheetdb->create([ ['name'=>'Chris','age'=>'34'], ['name'=>'Amanda','age'=>'29'], ]); // update a row for name="Chris" $sheetdb->update('name','Chris',['age'=>'33']); // will delete all rows with name = "Chris" $sheetdb->delete('name','Chris');

PHP without any libraries

If you are not using the composer, you can use file_get_contents like so:

<?php function requestSheetdb($url, $method = 'GET', $data = []) { $options = array( 'http' => array( 'header' => 'Content-type: application/x-www-form-urlencoded', 'method' => strtoupper($method), 'content' => http_build_query([ 'data' => $data ]) ) ); try { $raw = @file_get_contents($url, false, stream_context_create($options)); $result = json_decode($raw); } catch (Exception $e) { return false; } return $result; } // returns all spreadsheets data $content = requestSheetdb('https://sheetdb.io/api/v1/58f61be4dda40'); // returns when name="Steve" AND age=22 $content = requestSheetdb('https://sheetdb.io/api/v1/58f61be4dda40/search?name=Steve&age=22'); // insert one row $content = requestSheetdb('https://sheetdb.io/api/v1/58f61be4dda40', 'POST', ['name'=>'Mark','age'=>'35']); // etc.

Have question?

If you have any questions feel free to ask us via chat or .