2013-01-25 3 views
1

google-api-php-client (API v3)을 사용하여 페이지의 내 Google 캘린더에서 이벤트 목록을 표시합니다. 그것은 작동하지만, 내가 페이지를로드 할 때마다 API 호출을하지 않아도 (달력이 많이 변경되지 않음) 결과를 디스크에 로컬로 캐시하고 싶습니다. 결과를 캐시 할 수 있습니까?PHP API에서 Google 캘린더 이벤트 캐시 GET 요청

Google_FileCache.php라는 파일이 있는데 원하는대로 할 수있는 것처럼 보이는 Google_FileCache() 클래스가 있지만 누구나 사용하고있는 설명서가 전혀 없습니다. 이 작업을 수행하는 방법을 찾고 다른 사람에 대한 참고

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

감사합니다. –

답변

1

그냥과 :

다음은 내 지금까지 코드입니다. Google_FileCache 클래스를 사용하여 반환 된 GET 요청을 캐시하는 방법을 알아 냈습니다. 아래는 내 솔루션입니다.

// 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

Google이 가벼운 API 호출을 제공하여 마지막 API 호출 이후 변경 사항이 있는지 여부를 확인할 수 있다면 좋을 것입니다. 그런 다음 없었 으면이를 사용할 수 있습니다. –