Skip to content

Instantly share code, notes, and snippets.

@karlkranich
Last active July 17, 2018 14:49

Revisions

  1. karlkranich revised this gist Mar 6, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions apitest.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php
    // apitest.php
    // by Karl Kranich - karl.kranich.org
    // version 3 - added query section
    // version 3.1 - edited query section

    require_once realpath(dirname(__FILE__) . '/vendor/autoload.php');
    include_once "google-api-php-client/examples/templates/base.php";
    @@ -127,10 +127,11 @@

    // Section 7: Uncomment to query for a subset of rows and parse data with SimpleXML
    // $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full?sq=quantity>9";
    // $myQuery = 'quantity>9'; // and here is an example with a space in it: $myQuery = 'gear="mifi device"';
    // $method = 'GET';
    // $headers = ["Authorization" => "Bearer $accessToken", "GData-Version" => "3.0"];
    // $httpClient = new GuzzleHttp\Client(['headers' => $headers]);
    // $resp = $httpClient->request($method, $url);
    // $resp = $httpClient->request($method, $url, ['query' => ['sq' => $myQuery]]);
    // $body = $resp->getBody()->getContents();
    // $tableXML = simplexml_load_string($body);
    // echo "Rows:\n";
  2. karlkranich revised this gist Mar 6, 2016. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions apitest.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    <?php
    // apitest.php
    // by Karl Kranich - karl.kranich.org
    // version 3 - added query section

    require_once realpath(dirname(__FILE__) . '/vendor/autoload.php');
    include_once "google-api-php-client/examples/templates/base.php";
    @@ -121,3 +124,24 @@
    // echo "$colName : $colValue\n";
    // }
    // }

    // Section 7: Uncomment to query for a subset of rows and parse data with SimpleXML
    // $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full?sq=quantity>9";
    // $method = 'GET';
    // $headers = ["Authorization" => "Bearer $accessToken", "GData-Version" => "3.0"];
    // $httpClient = new GuzzleHttp\Client(['headers' => $headers]);
    // $resp = $httpClient->request($method, $url);
    // $body = $resp->getBody()->getContents();
    // $tableXML = simplexml_load_string($body);
    // echo "Rows:\n";
    // foreach ($tableXML->entry as $entry) {
    // $etag = $entry->attributes('gd', TRUE);
    // $id = $entry->id;
    // echo "etag: $etag\n";
    // echo "id: $id\n";
    // foreach ($entry->children('gsx', TRUE) as $column) {
    // $colName = $column->getName();
    // $colValue = $column;
    // echo "$colName : $colValue\n";
    // }
    // }
  3. karlkranich revised this gist Mar 3, 2016. No changes.
  4. karlkranich revised this gist Mar 3, 2016. No changes.
  5. karlkranich revised this gist Mar 3, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions apitest.php
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,10 @@
    $client->setApplicationName("Sheets API Testing");
    $client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);

    // Some people have reported needing to use the following setAuthConfig command
    // which requires the email address of your service account (you can get that from the json file)
    // $client->setAuthConfig(["type" => "service_account", "client_email" => "my-service-account@developer.gserviceaccount.com"]);

    // The file ID was copied from a URL while editing the sheet in Chrome
    $fileId = '15byt2tfdaHmaEpdwd4UYGWs70Eaej8edkQ2dS8x4mIk';

  6. karlkranich revised this gist Jan 10, 2016. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions apitest.php
    Original file line number Diff line number Diff line change
    @@ -96,3 +96,24 @@
    // $reason = $resp->getReasonPhrase();
    // echo "$code : $reason\n\n";
    // echo "$body\n";

    // Section 6: Uncomment to parse table data with SimpleXML
    // $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full";
    // $method = 'GET';
    // $headers = ["Authorization" => "Bearer $accessToken", "GData-Version" => "3.0"];
    // $httpClient = new GuzzleHttp\Client(['headers' => $headers]);
    // $resp = $httpClient->request($method, $url);
    // $body = $resp->getBody()->getContents();
    // $tableXML = simplexml_load_string($body);
    // echo "Rows:\n";
    // foreach ($tableXML->entry as $entry) {
    // $etag = $entry->attributes('gd', TRUE);
    // $id = $entry->id;
    // echo "etag: $etag\n";
    // echo "id: $id\n";
    // foreach ($entry->children('gsx', TRUE) as $column) {
    // $colName = $column->getName();
    // $colValue = $column;
    // echo "$colName : $colValue\n";
    // }
    // }
  7. karlkranich revised this gist Jan 7, 2016. 1 changed file with 42 additions and 68 deletions.
    110 changes: 42 additions & 68 deletions apitest.php
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,8 @@
    <?php
    /*
    * Copyright 2013 Google Inc.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */

    // Modified by Karl Kranich to demonstrate the Sheets API

    include_once "google-api-php-client/vendor/autoload.php";
    require_once realpath(dirname(__FILE__) . '/vendor/autoload.php');
    include_once "google-api-php-client/examples/templates/base.php";

    /************************************************
    Make an API request authenticated with a service
    account.
    ************************************************/

    $client = new Google_Client();

    /************************************************
    @@ -36,12 +14,8 @@
    somewhere you can get to it - though in real
    operations you'd want to make sure it wasn't
    accessible from the webserver!
    Make sure the Books API is enabled on this
    account as well, or the call will fail.
    ************************************************/

    // Karl chose putenv - edit this path if your json file is named differently or in a different folder than this file
    putenv('GOOGLE_APPLICATION_CREDENTIALS=service-account.json');
    putenv("GOOGLE_APPLICATION_CREDENTIALS=service-account-credentials.json");

    if ($credentials_file = checkServiceAccountCredentialsFile()) {
    // set the location manually
    @@ -56,69 +30,69 @@

    $client->setApplicationName("Sheets API Testing");
    $client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);
    $service = new Google_Service_Drive($client);

    // The file ID was copied from a URL while editing the sheet in Chrome
    $fileId = '15byt2tfdaHmaEpdwd4UYGWs70Eaej8edkQ2dS8x4mIk'; // replace with your file identifier
    $fileId = '15byt2tfdaHmaEpdwd4UYGWs70Eaej8edkQ2dS8x4mIk';

    // Access Token is used for Steps 2 and beyond
    $tokenArray = $client->fetchAccessTokenWithAssertion();
    $accessToken = $tokenArray["access_token"];

    // Section 1: Uncomment to get file metadata with the drive service
    // This is also the service that would be used to create a new spreadsheet file
    $service = new Google_Service_Drive($client);
    $results = $service->files->get($fileId);
    var_dump($results);

    // Section 2: Uncomment to get list of worksheets
    // $url = "https://spreadsheets.google.com/feeds/worksheets/$fileId/private/full";
    // $method = 'GET';
    // $httpClient = new GuzzleHttp\Client();
    // $client->authorize($httpClient);
    // $request = $httpClient->createRequest($method, $url);
    // $response = $httpClient->send($request);
    // $statusCode = $response->getStatusCode();
    // $reason = $response->getReasonPhrase();
    // $body = $response->getBody();
    // echo "$statusCode, $reason\n\n$body\n";
    // $headers = ["Authorization" => "Bearer $accessToken"];
    // $httpClient = new GuzzleHttp\Client(['headers' => $headers]);
    // $resp = $httpClient->request($method, $url);
    // $body = $resp->getBody()->getContents();
    // $code = $resp->getStatusCode();
    // $reason = $resp->getReasonPhrase();
    // echo "$code : $reason\n\n";
    // echo "$body\n";

    // Section 3: Uncomment to get the table data
    // Found the following URL in the output from section 2, looking for href after link rel='http://schemas.google.com/g/2005#feed'
    // $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full";
    // $method = 'GET';
    // $httpClient = new GuzzleHttp\Client();
    // $client->authorize($httpClient);
    // $request = $httpClient->createRequest($method, $url);
    // $response = $httpClient->send($request);
    // $statusCode = $response->getStatusCode();
    // $reason = $response->getReasonPhrase();
    // $body = $response->getBody();
    // echo "$statusCode, $reason\n\n$body\n";
    // $headers = ["Authorization" => "Bearer $accessToken", "GData-Version" => "3.0"];
    // $httpClient = new GuzzleHttp\Client(['headers' => $headers]);
    // $resp = $httpClient->request($method, $url);
    // $body = $resp->getBody()->getContents();
    // $code = $resp->getStatusCode();
    // $reason = $resp->getReasonPhrase();
    // echo "$code : $reason\n\n";
    // echo "$body\n";

    // Section 4: Uncomment to add a row to the sheet
    // The same URL can be used to POST new rows to the spreadsheet
    // $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full";
    // $method = 'POST';
    // $headers = ['Content-Type' => 'application/atom+xml'];
    // $postBody = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"><gsx:gear>more gear</gsx:gear><gsx:quantity>99</gsx:quantity></entry>';
    // $httpClient = new GuzzleHttp\Client();
    // $client->authorize($httpClient);
    // $request = $httpClient->createRequest($method, $url, ['headers' => $headers, 'body' => $postBody]);
    // $response = $httpClient->send($request);
    // $statusCode = $response->getStatusCode();
    // $reason = $response->getReasonPhrase();
    // $body = $response->getBody();
    // echo "$statusCode, $reason\n\n$body\n";
    // $headers = ["Authorization" => "Bearer $accessToken", 'Content-Type' => 'application/atom+xml'];
    // $postBody = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"><gsx:gear>more gear</gsx:gear><gsx:quantity>44</gsx:quantity></entry>';
    // $httpClient = new GuzzleHttp\Client(['headers' => $headers]);
    // $resp = $httpClient->request($method, $url, ['body' => $postBody]);
    // $body = $resp->getBody()->getContents();
    // $code = $resp->getStatusCode();
    // $reason = $resp->getReasonPhrase();
    // echo "$code : $reason\n\n";
    // echo "$body\n";

    // Section 5: Uncomment to edit a row
    // You'll need to get the etag and row ID, and send a PUT request to the edit URL
    // $rowid = 'cre1l'; // got this and the etag from the table data output from section 3
    // $etag = 'NQ8VCRBLVCt7ImA.';
    // $etag = 'NQ8SVE8fDSt7ImA.';
    // $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full/$rowid";
    // $method = 'PUT';
    // $headers = ['Content-Type' => 'application/atom+xml', 'GData-Version' => '3.0'];
    // $headers = ["Authorization" => "Bearer $accessToken", 'Content-Type' => 'application/atom+xml', 'GData-Version' => '3.0'];
    // $postBody = "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:gsx=\"http://schemas.google.com/spreadsheets/2006/extended\" xmlns:gd=\"http://schemas.google.com/g/2005\" gd:etag='&quot;$etag&quot;'><id>https://spreadsheets.google.com/feeds/list/$fileid/od6/$rowid</id><gsx:gear>phones</gsx:gear><gsx:quantity>6</gsx:quantity></entry>";
    // $httpClient = new GuzzleHttp\Client();
    // $client->authorize($httpClient);
    // $request = $httpClient->createRequest($method, $url, ['headers' => $headers, 'body' => $postBody]);
    // $response = $httpClient->send($request);
    // $statusCode = $response->getStatusCode();
    // $reason = $response->getReasonPhrase();
    // $body = $response->getBody();
    // echo "$statusCode, $reason\n\n$body\n";
    // $httpClient = new GuzzleHttp\Client(['headers' => $headers]);
    // $resp = $httpClient->request($method, $url, ['body' => $postBody]);
    // $body = $resp->getBody()->getContents();
    // $code = $resp->getStatusCode();
    // $reason = $resp->getReasonPhrase();
    // echo "$code : $reason\n\n";
    // echo "$body\n";
  8. karlkranich revised this gist Nov 9, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion apitest.php
    Original file line number Diff line number Diff line change
    @@ -112,7 +112,7 @@
    // $etag = 'NQ8VCRBLVCt7ImA.';
    // $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full/$rowid";
    // $method = 'PUT';
    // $headers = ["Authorization" => "Bearer $accessToken", 'Content-Type' => 'application/atom+xml', 'GData-Version' => '3.0'];
    // $headers = ['Content-Type' => 'application/atom+xml', 'GData-Version' => '3.0'];
    // $postBody = "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:gsx=\"http://schemas.google.com/spreadsheets/2006/extended\" xmlns:gd=\"http://schemas.google.com/g/2005\" gd:etag='&quot;$etag&quot;'><id>https://spreadsheets.google.com/feeds/list/$fileid/od6/$rowid</id><gsx:gear>phones</gsx:gear><gsx:quantity>6</gsx:quantity></entry>";
    // $httpClient = new GuzzleHttp\Client();
    // $client->authorize($httpClient);
  9. karlkranich revised this gist Nov 5, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion apitest.php
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@
    account as well, or the call will fail.
    ************************************************/

    // Karl chose putenv
    // Karl chose putenv - edit this path if your json file is named differently or in a different folder than this file
    putenv('GOOGLE_APPLICATION_CREDENTIALS=service-account.json');

    if ($credentials_file = checkServiceAccountCredentialsFile()) {
  10. karlkranich revised this gist Nov 4, 2015. No changes.
  11. karlkranich created this gist Nov 4, 2015.
    124 changes: 124 additions & 0 deletions apitest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,124 @@
    <?php
    /*
    * Copyright 2013 Google Inc.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */

    // Modified by Karl Kranich to demonstrate the Sheets API

    include_once "google-api-php-client/vendor/autoload.php";
    include_once "google-api-php-client/examples/templates/base.php";

    /************************************************
    Make an API request authenticated with a service
    account.
    ************************************************/

    $client = new Google_Client();

    /************************************************
    ATTENTION: Fill in these values, or make sure you
    have set the GOOGLE_APPLICATION_CREDENTIALS
    environment variable. You can get these credentials
    by creating a new Service Account in the
    API console. Be sure to store the key file
    somewhere you can get to it - though in real
    operations you'd want to make sure it wasn't
    accessible from the webserver!
    Make sure the Books API is enabled on this
    account as well, or the call will fail.
    ************************************************/

    // Karl chose putenv
    putenv('GOOGLE_APPLICATION_CREDENTIALS=service-account.json');

    if ($credentials_file = checkServiceAccountCredentialsFile()) {
    // set the location manually
    $client->setAuthConfig($credentials_file);
    } elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
    // use the application default credentials
    $client->useApplicationDefaultCredentials();
    } else {
    echo missingServiceAccountDetailsWarning();
    exit;
    }

    $client->setApplicationName("Sheets API Testing");
    $client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);
    $service = new Google_Service_Drive($client);

    // The file ID was copied from a URL while editing the sheet in Chrome
    $fileId = '15byt2tfdaHmaEpdwd4UYGWs70Eaej8edkQ2dS8x4mIk'; // replace with your file identifier

    // Section 1: Uncomment to get file metadata with the drive service
    // This is also the service that would be used to create a new spreadsheet file
    $results = $service->files->get($fileId);
    var_dump($results);

    // Section 2: Uncomment to get list of worksheets
    // $url = "https://spreadsheets.google.com/feeds/worksheets/$fileId/private/full";
    // $method = 'GET';
    // $httpClient = new GuzzleHttp\Client();
    // $client->authorize($httpClient);
    // $request = $httpClient->createRequest($method, $url);
    // $response = $httpClient->send($request);
    // $statusCode = $response->getStatusCode();
    // $reason = $response->getReasonPhrase();
    // $body = $response->getBody();
    // echo "$statusCode, $reason\n\n$body\n";

    // Section 3: Uncomment to get the table data
    // Found the following URL in the output from section 2, looking for href after link rel='http://schemas.google.com/g/2005#feed'
    // $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full";
    // $method = 'GET';
    // $httpClient = new GuzzleHttp\Client();
    // $client->authorize($httpClient);
    // $request = $httpClient->createRequest($method, $url);
    // $response = $httpClient->send($request);
    // $statusCode = $response->getStatusCode();
    // $reason = $response->getReasonPhrase();
    // $body = $response->getBody();
    // echo "$statusCode, $reason\n\n$body\n";

    // Section 4: Uncomment to add a row to the sheet
    // The same URL can be used to POST new rows to the spreadsheet
    // $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full";
    // $method = 'POST';
    // $headers = ['Content-Type' => 'application/atom+xml'];
    // $postBody = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"><gsx:gear>more gear</gsx:gear><gsx:quantity>99</gsx:quantity></entry>';
    // $httpClient = new GuzzleHttp\Client();
    // $client->authorize($httpClient);
    // $request = $httpClient->createRequest($method, $url, ['headers' => $headers, 'body' => $postBody]);
    // $response = $httpClient->send($request);
    // $statusCode = $response->getStatusCode();
    // $reason = $response->getReasonPhrase();
    // $body = $response->getBody();
    // echo "$statusCode, $reason\n\n$body\n";

    // Section 5: Uncomment to edit a row
    // You'll need to get the etag and row ID, and send a PUT request to the edit URL
    // $rowid = 'cre1l'; // got this and the etag from the table data output from section 3
    // $etag = 'NQ8VCRBLVCt7ImA.';
    // $url = "https://spreadsheets.google.com/feeds/list/$fileId/od6/private/full/$rowid";
    // $method = 'PUT';
    // $headers = ["Authorization" => "Bearer $accessToken", 'Content-Type' => 'application/atom+xml', 'GData-Version' => '3.0'];
    // $postBody = "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:gsx=\"http://schemas.google.com/spreadsheets/2006/extended\" xmlns:gd=\"http://schemas.google.com/g/2005\" gd:etag='&quot;$etag&quot;'><id>https://spreadsheets.google.com/feeds/list/$fileid/od6/$rowid</id><gsx:gear>phones</gsx:gear><gsx:quantity>6</gsx:quantity></entry>";
    // $httpClient = new GuzzleHttp\Client();
    // $client->authorize($httpClient);
    // $request = $httpClient->createRequest($method, $url, ['headers' => $headers, 'body' => $postBody]);
    // $response = $httpClient->send($request);
    // $statusCode = $response->getStatusCode();
    // $reason = $response->getReasonPhrase();
    // $body = $response->getBody();
    // echo "$statusCode, $reason\n\n$body\n";