2013-01-25 5 views
1

J'utilise le client google-api-php (API v3) pour afficher une liste d'événements de mon Google Agenda sur une page. Cela fonctionne, mais je voudrais mettre en cache les résultats localement sur le disque afin que je n'ai pas besoin de faire un appel d'API chaque fois que la page est chargée (le calendrier ne change pas beaucoup). Est-il possible de mettre en cache les résultats?Cache Google Calendar Événements de PHP API Demande GET

Il y a un fichier appelé Google_FileCache.php qui a une classe Google_FileCache() qui semble faire ce que je veux, mais je ne trouve aucune documentation à ce sujet, est-ce que quelqu'un l'a utilisé?

Voici mon code à ce jour:

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>'; 
} 
+0

Merci pour cet excellent morceau de code. –

Répondre

1

Juste et votre information pour tout le monde à la recherche comment faire cela. J'ai compris comment utiliser la classe Google_FileCache pour mettre en cache la requête GET retournée. Voici ma 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

Il serait préférable que google fournisse un appel d'API léger pour voir s'il y a eu des changements ou non depuis le dernier appel d'API et nous pourrions l'utiliser s'il n'y en avait pas eu. –

Questions connexes