【问题标题】:Which PHP Script File is needed for ical() class in Google Calendar PHP API v3Google Calendar PHP API v3 中的 ical() 类需要哪个 PHP 脚本文件
【发布时间】:2015-01-23 22:49:42
【问题描述】:

我正在构建的 PHP 脚本出现错误:

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

这是我的脚本文件中的一个 sn-p:

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 & API's > Credentials > Service Account > Email Address 以确保安全

【问题讨论】:

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


    【解决方案1】:

    如果您想使用服务帐户,则您的代码会关闭很多。我无法测试我的本地网络服务器正在运行的这段代码,但它应该很接近,你可能需要修改 $service-&gt;Events-&gt;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 目录,您可以删除上面不需要的所有内容。代码是根据我在PHP 中显示的唯一教程编辑的。

    【讨论】:

    • 1. print $service 只是检查它的连接。 2. 尝试将其更改为 $service->events->list();可能只是检查了客户端库。
    • 等待那行不通,您将需要日历 ID。刚刚编辑了您将不得不使用 calendarlist 来查找 id。
    • 我不认为MY-CAL是一个更像“311gpgricpq0mjpi567806321g@group.calendar.google.com”的日历id
    • 嘿猜猜看...我 print_r()'d 了 $events 并且低头一看——事件出现了。
    • 谢谢...我在开玩笑。我知道谷歌不会那样做。他们忙于弃用和发布,没有明确的方法让人们了解如何使用和实施他们在那里发布的内容。我们这些“懒鬼”必须等待像你这样的人发光。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多