├── .gitignore ├── README.md ├── composer.json └── src ├── Connection.php └── SheetDB.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/* 2 | *.orig 3 | .buildpath 4 | .project 5 | .settings/* 6 | .idea/* 7 | composer.lock 8 | test/* 9 | *~ 10 | /.settings/ 11 | /.php_cs.cache 12 | .DS_Store 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP SheetDB Class 2 | 3 | ### Installation 4 | 5 | The SheetDB PHP Library is available through [Composer](https://getcomposer.org/) 6 | 7 | ``` 8 | composer require sheetdb/sheetdb-php 9 | ``` 10 | 11 | ### Test spreadsheet 12 | 13 | At any time you can play with our test API here: https://sheetdb.io/api/v1/58f61be4dda40 14 | 15 | You can also go to Google Sheets and play with it: https://docs.google.com/spreadsheets/u/2/d/1mrsgBk4IAdSs8Ask5H1z3bWYDlPTKplDIU_FzyktrGk/edit **The spreadsheet resets every hour.** 16 | 17 | ### Basic usage 18 | 19 | In order to instantiate SheetDB connection you need to give an api id. You can find it in [SheetDB Dashboard](https://sheetdb.io). 20 | 21 | ```php 22 | require('vendor/autoload.php'); 23 | use SheetDB\SheetDB; 24 | 25 | $sheetdb = new SheetDB('58f61be4dda40'); 26 | $response = $sheetdb->get(); // returns all spreadsheets data 27 | $response = $sheetdb->keys(); // returns all spreadsheets key names 28 | $response = $sheetdb->name(); // returns name of a spreadsheet document 29 | ``` 30 | 31 | ### Searching 32 | 33 | You can use search method to find only specific rows. You have 2 options. Search rows that meets all of the conditions or search rows that meets at least one of the conditions. 34 | 35 | You can use second parameter if you want the search to be case sensitive (it is boolean). By default it is not case sensitive. 36 | 37 | ```php 38 | $response = $sheetdb->search(['name'=>'Steve','age'=>'22']); // returns when name="Steve" AND age=22 39 | $response = $sheetdb->searchOr(['name'=>'Steve','age'=>'19']); // returns when name="Steve" OR age=19 40 | 41 | $response = $sheetdb->search(['name'=>'Steve'], true); // returns when name="Steve", this query is case sensitive 42 | ``` 43 | 44 | ### Creating a row(s) 45 | 46 | If you want to create a new row you'll need to pass an array with key_name => value. If you want to create multiple rows that should be an array of arrays. 47 | 48 | ```php 49 | $sheetdb->create(['name'=>'Mark','age'=>'35']); 50 | $sheetdb->create([ 51 | ['name'=>'Chris','age'=>'34'], 52 | ['name'=>'Amanda','age'=>'29'], 53 | ]); 54 | ``` 55 | 56 | ### Updating a row(s) 57 | 58 | First two parameters are key and value that need to match, third argument is array similar to the create one. 59 | 60 | Fourth parameter is optional. If it's true not specified values will be emptied. 61 | 62 | ```php 63 | $sheetdb->update('name','Chris',['age'=>'33']); // it will change only age 64 | $sheetdb->update('name','Chris',['age'=>'33'],true); // it will update age to 33 but all other values will be emptied, in this case a name 65 | ``` 66 | 67 | ### Update the batch of rows in the spreadsheet 68 | 69 | Update for various queries. First argument is data itself. Each object in it should have a query key with the actual query (for example, "id=5"), and the remaining keys will be updated, as in a regular PATCH / PUT request. 70 | 71 | Second parameter is optional. If it's true not specified values will be emptied. 72 | 73 | ```php 74 | $sheetdb->batchUpdate([ 75 | [ 76 | 'query' => 'id=1', 77 | 'age' => '33', 78 | ], 79 | [ 80 | 'query' => 'id=2', 81 | 'age' => '52', 82 | ] 83 | ]); 84 | ``` 85 | 86 | ### Delete a row(s) 87 | 88 | Just like in update first two parameters are key and value. Every row that will match query will be deleted. 89 | 90 | ```php 91 | $sheetdb->delete('name','Chris'); // will delete all rows with name = "Chris" 92 | ``` 93 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sheetdb/sheetdb-php", 3 | "description": "PHP Library for accessing google spreadsheet content via SheetDB. https://sheetdb.io", 4 | "require": { 5 | "php": ">=5.5" 6 | }, 7 | "license": "MIT", 8 | "autoload": { 9 | "psr-4": { 10 | "SheetDB\\": "src/" 11 | } 12 | }, 13 | "authors": [ 14 | { 15 | "name": "SheetDB", 16 | "email": "support@sheetdb.io" 17 | } 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /src/Connection.php: -------------------------------------------------------------------------------- 1 | url = $url; 19 | $this->sheet = $sheet; 20 | } 21 | 22 | public function setUrl($url) { 23 | $this->url = $url; 24 | } 25 | 26 | public function getUrl() { 27 | return $this->url; 28 | } 29 | 30 | public function setQueryParams($queryParams) { 31 | $this->queryParams = $queryParams; 32 | } 33 | 34 | public function getQueryParams() { 35 | return $this->queryParams; 36 | } 37 | 38 | public function addToQueryParams($param) { 39 | $this->queryParams = array_merge($this->queryParams,$param); 40 | } 41 | 42 | public function resetQueryParams() { 43 | $this->queryParams = []; 44 | } 45 | 46 | /** 47 | * This method prepares the query parameters and sets the final url. 48 | * @return $result of request specified in sheetdb documentation 49 | * OR false if something went wrong 50 | */ 51 | public function makeRequest($method = 'get', $data = []) { 52 | 53 | $options = array( 54 | 'http' => array( 55 | 'header' => 'Content-type: application/x-www-form-urlencoded', 56 | 'method' => strtoupper($method), 57 | 'content' => http_build_query([ 58 | 'data' => $data 59 | ]) 60 | ) 61 | ); 62 | 63 | $this->prepareQueryUrl(); 64 | 65 | try { 66 | $raw = @file_get_contents($this->url, false, stream_context_create($options)); 67 | $result = json_decode($raw); 68 | } catch (Exception $e) { 69 | return false; 70 | } 71 | 72 | return $result; 73 | } 74 | 75 | /** 76 | * If queryParams are set, updates url with http_build_query 77 | */ 78 | private function prepareQueryUrl() { 79 | $url = $this->url; 80 | 81 | if ($this->sheet) { 82 | $this->queryParams['sheet'] = $this->sheet; 83 | } 84 | 85 | if ($this->queryParams) { 86 | $url .= '/?'; 87 | $url .= http_build_query($this->queryParams); 88 | $this->url = $url; 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/SheetDB.php: -------------------------------------------------------------------------------- 1 | api_id = $api_id; 25 | $this->connection = new Connection($this->handlerUrl(), $sheet); 26 | } 27 | 28 | /** 29 | * Get all of the spreadsheet data 30 | * @return array|bool Results or false 31 | */ 32 | public function get() { 33 | $this->connection->resetQueryParams(); 34 | $this->connection->setUrl($this->handlerUrl()); 35 | return $this->connection->makeRequest(); 36 | } 37 | 38 | /** 39 | * Get all keys of the spreadsheet 40 | * @return array|bool Array of keys or false 41 | */ 42 | public function keys() { 43 | $this->connection->resetQueryParams(); 44 | $this->connection->setUrl($this->handlerUrl('/keys')); 45 | return $this->connection->makeRequest('get'); 46 | } 47 | 48 | /** 49 | * Get document name of the spreadsheet 50 | * @return string|bool Name of document or false 51 | */ 52 | public function name() { 53 | $this->connection->resetQueryParams(); 54 | $this->connection->setUrl($this->handlerUrl('/name')); 55 | $response = $this->connection->makeRequest('get'); 56 | if ($response && isset($response->name)){ 57 | return $response->name; 58 | } 59 | return false; 60 | } 61 | 62 | /** 63 | * Get count of rows in spreadsheet (without first row) 64 | * @return int Number of rows 65 | */ 66 | public function count() { 67 | $this->connection->resetQueryParams(); 68 | $this->connection->setUrl($this->handlerUrl('/count')); 69 | $response = $this->connection->makeRequest('get'); 70 | if ($response && isset($response->rows)){ 71 | return $response->rows; 72 | } 73 | return false; 74 | } 75 | 76 | /** 77 | * Search within spreadsheet 78 | * @param array $query 79 | * @param bool $caseSensitive 80 | * @return array|bool Results of search or false 81 | */ 82 | public function search(array $query, $caseSensitive = false) { 83 | $this->connection->resetQueryParams(); 84 | $this->connection->addToQueryParams($query); 85 | $this->connection->addToQueryParams(['casesensitive' => $caseSensitive]); 86 | $this->connection->setUrl($this->handlerUrl('/search')); 87 | return $this->connection->makeRequest('get'); 88 | } 89 | 90 | /** 91 | * Search within spreadsheet, if any parameter is true, matches will 92 | * be in a response 93 | * @param array $query 94 | * @param bool $caseSensitive 95 | * @return array|bool Results of search or false 96 | */ 97 | public function searchOr(array $query, $caseSensitive = false) { 98 | $this->connection->resetQueryParams(); 99 | $this->connection->addToQueryParams($query); 100 | $this->connection->addToQueryParams(['casesensitive' => $caseSensitive]); 101 | $this->connection->setUrl($this->handlerUrl('/search_or')); 102 | return $this->connection->makeRequest('get'); 103 | } 104 | 105 | /** 106 | * Create a row(s) in spreadsheet 107 | * @param array $data 108 | * @return int|bool Count of rows created or false 109 | */ 110 | public function create(array $data) { 111 | $this->connection->resetQueryParams(); 112 | $this->connection->setUrl($this->handlerUrl()); 113 | $response = $this->connection->makeRequest('post', $data); 114 | if ($response && isset($response->created)){ 115 | return $response->created; 116 | } 117 | return false; 118 | } 119 | 120 | /** 121 | * Update a row(s) in spreadsheet 122 | * @param string $columnName 123 | * @param string $value 124 | * @param array $data 125 | * @param bool $putMethod If it's true, make PUT request, otherwise PATCH 126 | * @return int|bool Count of rows updated or false 127 | */ 128 | public function update($columnName, $value, array $data, $putMethod = false) { 129 | $this->connection->resetQueryParams(); 130 | $this->connection->setUrl($this->handlerUrl('/' . $columnName . '/' . $value)); 131 | $response = $this->connection->makeRequest($putMethod === true ? 'put' : 'patch', [$data]); 132 | if ($response && isset($response->updated)){ 133 | return $response->updated; 134 | } 135 | return false; 136 | } 137 | 138 | /** 139 | * Update the batch of rows in the spreadsheet 140 | * @param array $data 141 | * @param bool $putMethod If it's true, make PUT request, otherwise PATCH 142 | * @return int|bool Count of rows updated or false 143 | */ 144 | public function batchUpdate(array $data, $putMethod = false) { 145 | $this->connection->resetQueryParams(); 146 | $this->connection->setUrl($this->handlerUrl('/batch_update')); 147 | $response = $this->connection->makeRequest($putMethod === true ? 'put' : 'patch', $data); 148 | if ($response && isset($response->updated)){ 149 | return $response->updated; 150 | } 151 | return false; 152 | } 153 | 154 | /** 155 | * Delete a row(s) in spreadsheet 156 | * @param string $columnName 157 | * @param string $value 158 | * @return int|bool Count of rows deleted or false 159 | */ 160 | public function delete($columnName, $value) { 161 | $this->connection->resetQueryParams(); 162 | $this->connection->setUrl($this->handlerUrl('/' . $columnName . '/' . $value)); 163 | $response = $this->connection->makeRequest('delete'); 164 | if ($response && isset($response->deleted)){ 165 | return $response->deleted; 166 | } 167 | return false; 168 | } 169 | 170 | /** 171 | * Helper method that returns url with appended content 172 | * @return string 173 | */ 174 | private function handlerUrl($append = '') { 175 | return self::BASE_URL . $this->api_id . $append; 176 | } 177 | 178 | } 179 | --------------------------------------------------------------------------------