Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:04:35+00:00 2026-06-18T00:04:35+00:00

I am using the google-api-php-client (API v3) to display a list of events from

  • 0

I am using the google-api-php-client (API v3) to display a list of events from my Google Calendar on a page. It is working, however I would like to cache the results locally on the disk so I don’t have to make an API call every time the page is loaded (the calendar doesn’t change much). Is it possible to cache the results?

There is a file called Google_FileCache.php which has a Google_FileCache() class that looks like it might do what I want, but I can’t find any documentation for it at all, has anyone used it?

Here is my code so far:

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
$CLIENT_ID = '{my client ID}';
$SERVICE_ACCOUNT_NAME = '{my service account name}';
$KEY_FILE = '{my p12 key file}';
$client = new Google_Client();
$client->setApplicationName("Public Calendar");
$client->setUseObjects(true);
if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}
$key = file_get_contents($KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
    $SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/calendar', "https://www.googleapis.com/auth/calendar.readonly"),
    $key)
);
$client->setClientId($CLIENT_ID);
$service = new Google_CalendarService($client);
if ($client->getAccessToken()) {
    $_SESSION['token'] = $client->getAccessToken();
}
$rightNow = date('c');
$params = array('singleEvents' => 'true', 'orderBy' => 'startTime', 'timeMin' => $rightNow);
$events = $service->events->listEvents('primary', $params);
foreach ($events->getItems() as $event) {
    echo '<p>'.$event->getSummary().'</p>';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:04:35+00:00Added an answer on June 18, 2026 at 12:04 am

    Just and FYI for everyone else looking how to do this. I figured out how to use the Google_FileCache class to cache the returned GET request. Below is my solution:

    // Files to make API Call
    require_once 'google-api-php-client/src/Google_Client.php';
    require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
    // Files to cache the API Call
    require_once 'google-api-php-client/src/cache/Google_Cache.php';
    require_once 'google-api-php-client/src/cache/Google_FileCache.php';
    // Google Developer info for gmail.com Service API account
    $CLIENT_ID = '{My Client ID}';
    $SERVICE_ACCOUNT_NAME = '{My Service Account Name}';
    // Make sure you keep your key.p12 file in a secure location, and isn't readable by others.
    $KEY_FILE = '{My .p12 Key File}';
    $client = new Google_Client(); // Start API call
    $client->setApplicationName("Public Calendar");
    $client->setUseObjects(true); // Need this to return it as an object array
    // Checking to see that we are authenticated (OAuth2) to make API to calendar
    if (isset($_SESSION['token'])) {
     $client->setAccessToken($_SESSION['token']);
    }
    // Load the key in PKCS 12 format (you need to download this from the Google API Console when the service account was created.)
    $key = file_get_contents($KEY_FILE);
    $client->setAssertionCredentials(new Google_AssertionCredentials(
        $SERVICE_ACCOUNT_NAME,
        array('https://www.googleapis.com/auth/calendar', "https://www.googleapis.com/auth/calendar.readonly"),
        $key)
    );
    $client->setClientId($CLIENT_ID); // Set client ID for my API call
    $service = new Google_CalendarService($client); // Start API call to Calendar
    //Save authentication token in session
    if ($client->getAccessToken()) {
      $_SESSION['token'] = $client->getAccessToken();
    }
    // Start Caching service
    $cache = new Google_FileCache();
    $cache_time = 43200; // 12 hours
    // If cache is there & younger than 12 hours then load cached data
    if($cache->get('events_cache', $cache_time)) {
        $events = $cache->get('events_cache');
        $file_status = "cached";
    }
    else {
        //If it is not then make Calendar API request to Google and get the results
        $rightNow = date('c');
        $params = array('singleEvents' => 'true', 'orderBy' => 'startTime', 'timeMin' => $rightNow, 'maxResults' => 5);
        $events = $service->events->listEvents('primary', $params);
        $cache->set('events_cache', $events); // Save results into cache
        $file_status = "live";
    }
    // Start collecting info to be returned
    echo '<!-- '.$file_status.' -->'; // So I can tell if the results are cached or not
    echo '<ul class="events">';
    foreach ($events->getItems() as $event) {
        // Make link to event and list event name
        echo '<li><a href="'.$event->getHtmlLink().'">'.$event->getSummary().'</a></li>';
    }
    echo '</ul>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the Google Api PHP client to Log the user in. I
I'm using the google-api-php-client to get the URL for a document on my Google
I'm using the Zend_Gdata_Photos PHP client to access the Google Picasa API, trying to
im using the Google Api PHP v0.5.0(http://code.google.com/p/google-api-php-client/) , to access to Google Analytics, im
I am using Google API - PHP client to connect to Google Analytics API.
I am using the Google Api PHP Client. I want to get the details
I'm using the QR google api to create QR codes but would like the
I'm using a Google Analytics API Class in PHP made by Doug Tan to
I've read through the Google Spreadsheets API PHP documentation. All examples are using Zend,
I'm using GAPI library (in PHP) for querying Google Analytics API. I request 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.