PHP Classes

How to Implement a PHP Google Sheets Access Solution Without Using Google Libraries - PHP OAuth Library package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP OAuth Library PHP OAuth Library   Blog PHP OAuth Library package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement a PH...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2024-03-05

Posted on: 2024-03-05

Package: PHP OAuth Library

Google Sheets is a viral tool used to create and edit spreadsheets.

Google provides an API to access spreadsheets created with Google Sheets.

Read this short introduction tutorial to learn how to access a spreadsheet using Google Sheets API via OAuth without using Google libraries.




Loaded Article

In this article you will learn:

1. What is the Google Sheets API

2. Why It Is a Good Idea to Use Google Sheets API

3. How Can You Access Google Spreadsheets via OAuth

4. How Can You Download or Install the PHP OAuth Library using PHP Composer Tool


1. What is the Google Sheets API

Google sheets is a well-known spreedsheet editor tool. It is similar to Microsoft Excel.

The Google Sheets API allows to perform operations with Google Sheets from external programs.

These external programs can be PHP scripts that run on any computer that has Internet access. These PHP scripts can send HTTP requests to the Google Sheets API and perform operations that can access or modify spreadsheets.

2. Why It Is a Good Idea to Use Google Sheets API

The Google Sheets API provides the power of using Google Sheets from any programming language that can access this API.

This way, you can automate tasks or even develop new tools that extend the power of Google Sheets.

3. How Can You Access Google Spreadsheets via OAuth

This is an introductory article to this topic. So here, you will only see how to implement a simple task to access the contents of a spreadsheet's cell created in Google Sheets.

The example below shows a basic PHP script that can access the Google Sheets API to access a spreadsheet in a given cell.

In future articles, I will give more details on how to set up all the details of a Google Sheets API application that you can use to create your own Google Sheets API applications.

After you adjust the configuration of the PHP script that you can see below, you will see a results pages like this:

Google Sheets API Access PHP Example

object(stdClass)#3 (3) {
  ["range"]=>
  string(9) "Sheet1!A1"
  ["majorDimension"]=>
  string(4) "ROWS"
  ["values"]=>
  array(1) {
    [0]=>
    array(1) {
      [0]=>
      string(16) "Hello PHP World!"
    }
  }
}
<?php

 
// Include the necessary class files directly or
 // vendor/autoload.php if you used composer to
 // install the OAuth package.
 
require('http.php');
 require(
'oauth_client.php');

 
$client = new oauth_client_class;
 
$client->server 'Google';

 
// Setup the URL of your Google Sheets API application
 
$client->redirect_uri 'https://'.$_SERVER['HTTP_HOST'].
  
dirname(strtok($_SERVER['REQUEST_URI'],'?')).
 
'/access_google_sheets_spreadsheet.php';

 
$client->client_id ''$application_line __LINE__;
 
$client->client_secret '';
 
 if(
strlen($client->client_id) == 0
 
|| strlen($client->client_secret) == 0)
  die(
'Please go to Google APIs console page '.
  
'https://console.cloud.google.com/apis/api/sheets.googleapis.com/'.
  
' in the API access tab, create a new client ID, and in'.
  
' the line '.$application_line.' set the client_id to '.
  
'Client ID and client_secret with Client Secret. '.
  
'The callback URL must be '.$client->redirect_uri.
  
' but make sure the domain is valid and can be resolved'.
  
' by a public DNS.');

 
/* API permissions
  */
 
$client->scope 'https://www.googleapis.com/auth/spreadsheets';
 if((
$success $client->Initialize()))
 {
  if((
$success $client->Process()))
  {
   if(
strlen($client->authorization_error))
   {
    
$client->error $client->authorization_error;
    
$success false;
   }
   elseif(
strlen($client->access_token))
   {
    
// Assign this variable to the identifier of
    // the spreadsheet you want to access.
    
$id '1UfCqfVTeGYB-So43a1QDJ2dEIS-aYY1kruYStTUmYOE';
    
$range 'A1';
    
$success $client->CallAPI(
     
str_replace(array(
       
'{id}',
       
'{range}',
      ),
      array(
       
$id,
       
$range
      
),
      
'https://sheets.googleapis.com/'.
      
'v4/spreadsheets/{id}/values/{range}',
     ),
     
'GET',
     array(),
     array(
'FailOnAccessError'=>true),
     
$sheet_range);
   }
  }
  
$success $client->Finalize($success);
 }
 if(
$client->exit)
  exit;
 if(
$success)
 {
?>
<!DOCTYPE html>
<html>
<head>
<title>Google Contacts API PHP Example</title>
</head>
<body>
<h1>Google Sheets API Access PHP Example</h1>
<?php
  
echo '<pre>';
  
var_dump($sheet_range);
  echo 
'</pre>';
?>
</body>
</html>
<?php
 
}
 else
 {
?>
<!DOCTYPE html>
<html>
<head>
<title>OAuth client error</title>
</head>
<body>
<h1>OAuth client error</h1>
<pre>Error: <?php echo HtmlSpecialChars($client->error); ?></pre>
</body>
</html>
<?php
 
}

?>

4. How Can You Download or Install the PHP OAuth Library using PHP Composer Tool

The PHP OAuth library used in this article's example script can be installed manually by downloading the package files from the download page.

This package requires the PHP HTTP client class, which can also be installed manually after downloading the PHP HTTP client class package from its download page.

All these packages can be installed automatically from the PHP Composer tool by following the instructions to install the PHP OAuth client package to set up your project composer.json file to use these packages.




You need to be a registered user or login to post a comment

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   PHP OAuth Library PHP OAuth Library   Blog PHP OAuth Library package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement a PH...