Google Calendar PHP API v3中的ical()类需要哪个PHP脚本文件

时间:2014-11-25 19:30:17

标签: google-calendar-api google-api-php-client

我在构建的PHP脚本中收到错误:

致命错误:第74行的/home/abc/public_html/app/mods/googleCalendar_3.0/cache_events.php中找不到类'ical'

以下是我的脚本文件中的代码段:

define('CLIENT_ID', 'ASDLJJLDSJLASDJLajdl;jdsljkASD;LKJASDLKJASD.apps.googleusercontent.com');

    require_once('autoload.php'); // 2014-11-24 part of /usr/local/lib/php/google-api-php-client
    require_once('/usr/local/lib/php/google-api-php-client/src/Google/Client.php'); // 2014-11-25
    require_once('/usr/local/lib/php/google-api-php-client/src/Google/Service/Calendar.php'); // 2014-11-25

    $ical = new ical('https://www.google.com/calendar/ical/CLIENT-ID/public/basic.ics');

    $eventListArray = array_filter($ical -> events(), "locationfilter");

    $eventCount = count($eventListArray);

    print_r($eventListArray); echo "<br>";
    echo "Event Count:" . $eventCount;echo "<br>";
    exit;

我只是想在公共日历中检索所有事件

注意:

日历是公开可见的

为了确保,我添加了我的Auth&amp; API的&gt;证书&gt;服务帐户&gt;电子邮件地址只是为了安全

1 个答案:

答案 0 :(得分:1)

如果您想使用服务帐户,您的代码会相当多。我不能测试这个代码我的本地网络服务器正在起作用但它应该很接近你可能需要调整$service->Events->list();部分这是一种猜测。确保您在相关日历上以用户身份添加了服务帐户电子邮件地址,并且该地址应该有效。

session_start();        
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';     
/************************************************   
 The following 3 values an befound in the setting   
 for the application you created on Google      
 Developers console.         Developers console.
 The Key file should be placed in a location     
 that is not accessable from the web. outside of 
 web root.       web root.

 In order to access your GA account you must    
 Add the Email address as a user at the     
 ACCOUNT Level in the GA admin.         
 ************************************************/
$client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com';
$Email_address = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.com';     
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';     
$client = new Google_Client();      
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);    
// seproate additional scopes with a comma   
$scopes ="https://www.googleapis.com/auth/calendar";    
$cred = new Google_Auth_AssertionCredentials(    
 $Email_address,         
 array($scopes),        
 $key        
 );     
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {        
 $client->getAuth()->refreshTokenWithAssertion($cred);      
}       
$service = new Google_Service_Calendar($client);

// you should only need to do this once print this and you will
// find the calendarId for the one you are looking for.
$calendars = $service->calendarList->list();

$events = $service->events->list($yourCalendarID);

注意:您需要的只是Google Dir,您可以删除上面您不需要的所有内容。代码是从我在PHP中显示的唯一教程编辑的。