0

I am getting an error in a PHP script I am building:

Fatal error: Class 'ical' not found in /home/abc/public_html/app/mods/googleCalendar_3.0/cache_events.php on line 74

Here is a snippet from my script file:

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;

I am simply trying to retrieve all events in my public calendar

Notes:

Calendar is publicly viewable

Just to make sure, I added my Auth & API's > Credentials > Service Account > Email Address to it just to be safe

H. Ferrence
  • 7,906
  • 31
  • 98
  • 161

1 Answers1

1

If you want to use a service account your code is off quite a bit. I cant test this code my local webserver is acting up but it should be close you may have to tweek the $service->Events->list(); part it was kind of a guess. Make sure that you have the Service account email address added as a user on the calendar in question and it should work.

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);

Note: all you need is the Google Dir you can remove everything above that you dont really need it. Code was edited from the only tutorial i have that shows this in PHP.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • using your code with my credentials @DaImTo I get these errors: **"Notice: Undefined property: Google_Service_Calendar::$Events in /home/abc/public_html/app/mods/googleCalendar_3.0/cache_events.php on line 99 Fatal error: Call to a member function list() on a non-object in /home/abc/public_html/app/mods/googleCalendar_3.0/cache_events.php on line 99"** – H. Ferrence Nov 25 '14 at 20:46
  • 1
    1. print $service just check that its connecting. 2. try and change it to $service->events->list(); It might be lower case just checked the Client lib. – Linda Lawton - DaImTo Nov 25 '14 at 20:49
  • 1
    wait thats not going to work your going to need the calendar Id. Just edited your going to have to use calendarlist to find the id. – Linda Lawton - DaImTo Nov 25 '14 at 20:52
  • I have about t&e 15-16 versions I've tried. One of them had this ` $results = $service->events->listEvents('MY-CAL');` I'll give it a try – H. Ferrence Nov 25 '14 at 20:56
  • Wild. I got a blank page... I mean, I did not get an error. I did get my opening echo displayed so the script did not fail it merely did not return anything without showing me any errors @DaImTo – H. Ferrence Nov 25 '14 at 20:59
  • 1
    I don't think that MY-CAL is a calendar id that is more like ""311gpgricpq0mjpi567806321g@group.calendar.google.com"" – Linda Lawton - DaImTo Nov 25 '14 at 20:59
  • ha ha...what was me blurring my credentials – H. Ferrence Nov 25 '14 at 21:00
  • 1
    Hey GUESS WHAT...I print_r()'d the $events and low and behold -- the events appeared. – H. Ferrence Nov 25 '14 at 21:00
  • OK I guess i need to turn this into a tutorial now – Linda Lawton - DaImTo Nov 25 '14 at 21:01
  • Now I just need to filter and format -- you're awesome @DaImTo -- I have been spinning my wheels on this since last Friday and have fallen way behind in my project work load. You just plugged the dam for me. Greatly appreciated! – H. Ferrence Nov 25 '14 at 21:02
  • No -- GOOGLE NEEDS TO MAKE A TUTORIAL. How they can deprecate things without a clear and concise migration path is beyond me. – H. Ferrence Nov 25 '14 at 21:03
  • Google Doesnt make the tutorials I do and then i get hits on my website :) Good luck with the project – Linda Lawton - DaImTo Nov 25 '14 at 21:06
  • 1
    Thanks...I was being facetious. I know that Google would not do that. They're too busy deprecating and launching with no clear way for people to understanding how to use and implement what they put out there. Us "slackers" have to wait for people like you to shine the light. Thanks again. – H. Ferrence Nov 25 '14 at 21:13
  • Be sure to include this in your tutorial @DaImTo -- http://stackoverflow.com/questions/27114798/how-to-create-a-project-in-google-developers-console – H. Ferrence Nov 25 '14 at 21:17
  • 1
    In case it slips your mind @DaImTo, please post a link back to your tutorial. Thanks. – H. Ferrence Nov 26 '14 at 12:24