Can't Insert Event

541 views
Skip to first unread message

AlexandraT

unread,
Jul 5, 2012, 6:03:27 AM7/5/12
to google-api...@googlegroups.com
Hello,

I try to insert an event into google calendar.
I use the basic script.



<?php
require_once 'api-php/src/apiClient.php';
require_once 'api-php/src/contrib/apiCalendarService.php';
session_start();

 $client = new apiClient();
 $client->setApplicationName("Google Calendar PHP Starter Application");
 $client->setClientId('myClientID');
 $client->setClientSecret('myClientsecret');
 $client->setRedirectUri('mypage');
 $client->setDeveloperKey('mykey');
 
$cal = new apiCalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
 
if ($client->getAccessToken()) {


  $calList = $cal->calendarList->listCalendarList();  
 
  print "********** ma connexion est OK ******************<br>"; 
 
  
  //******************************************************* TEST ****
$event = new Event();
$event->setSummary('titre de l'evenement');
$event->setLocation('La Rochelle');

$start = new EventDateTime();
$start->setDateTime('2012-07-05T10:00:00');
$event->setStart($start);

$end = new EventDateTime();
$end->setDateTime('2012-07-05T13:25:00');
$event->setEnd($end);

$attendee1 = new EventAttendee();
$attendee1->setEmail('mymail');
$attendees = array($attendee1);
$event->attendees = $attendees;

$createdEvent = $service->events->insert( $client, $event);

echo $createdEvent->getId();



  $_SESSION['token'] = $client->getAccessToken();
}
else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}


?>


this return :  Fatal error: Call to a member function insert() on a non-object in /mypage.php on line 78


What's the problem ?

Thanks

Chirag Shah

unread,
Jul 5, 2012, 1:35:30 PM7/5/12
to google-api...@googlegroups.com
Hey AlexandraT,

I have included comments inline. The major issue is that $service isn't defined in the included snippet (it was defined as $cal).

I hope this helps!


On Thu, Jul 5, 2012 at 5:03 AM, AlexandraT <alex....@mdpqualite.fr> wrote:
Hello,

I try to insert an event into google calendar.
I use the basic script.



<?php
require_once 'api-php/src/apiClient.php';
require_once 'api-php/src/contrib/apiCalendarService.php';
session_start();

 $client = new apiClient();
 $client->setApplicationName("Google Calendar PHP Starter Application");
 $client->setClientId('myClientID');
 $client->setClientSecret('myClientsecret');
 $client->setRedirectUri('mypage');
 $client->setDeveloperKey('mykey');
 
$cal = new apiCalendarService($client);
$service is refered to below, but is never defined. This line should be changed to:
$service = new apiCalendarService($client);

 
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
 
if ($client->getAccessToken()) {


  $calList = $cal->calendarList->listCalendarList();
Need to update this line to reflect the earlier change.
 
 
  print "********** ma connexion est OK ******************<br>"; 
 
  
  //******************************************************* TEST ****
$event = new Event();
$event->setSummary('titre de l'evenement');
$event->setLocation('La Rochelle');

$start = new EventDateTime();
$start->setDateTime('2012-07-05T10:00:00');
$event->setStart($start);

$end = new EventDateTime();
$end->setDateTime('2012-07-05T13:25:00');
$event->setEnd($end);

$attendee1 = new EventAttendee();
$attendee1->setEmail('mymail');
$attendees = array($attendee1);
$event->attendees = $attendees;

$createdEvent = $service->events->insert( $client, $event);
The first parameter of the insert method is the calendarId, and followed by the event.

It should look like:
$createdEvent = $service->events->insert('primary', $event);

AlexandraT

unread,
Jul 6, 2012, 6:14:28 AM7/6/12
to google-api...@googlegroups.com
Thanks a lot !!!
It's Work.

Reply all
Reply to author
Forward
0 new messages