Basic Retrival and Updating

21 views
Skip to first unread message

Eric Jensen

unread,
Aug 25, 2013, 10:26:59 AM8/25/13
to google-api...@googlegroups.com
Hi

I spent a few hours on this so I thought I'd write a note about it in case it was helpful for others.
The examples in the developer reference all use object oriented programming and I couldn't seem to get this to work. I'm using a Apache, PHP, MySQL installation on my computer and I've set everything up correctly with Google API projects.
The problem was one line of code:
$client->setUseObjects(true);
The script was connecting to Google Calendar, but was returning arrays, not array objects. This meant that simple code like 
$eventList->getItems();
did not function correctly. I could parse the list of items with
foreach( $eventList['items'] as $event) {
   //...
} 
 but the object oriented commands, such as the ones used to update events did not function. By adding the "setUseObjects" command all the object oriented commands started working.

Here is the script for getting the Events in a Calendar, finding a specific one and updating it's description (it is based on all the nice examples from the Google API references):
<?php
require_once 'Google_Client.php';//google-api-php-client/src/
require_once 'contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Simple Google Calendar Updater");
// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('your client id');
$client->setClientSecret('your client secret');
$client->setRedirectUri('your redirec uri');
$client->setDeveloperKey('your api key');
$client->setUseObjects(true);
$service = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
echo '<!DOCTYPE HTML><html><body>' . "\n";

//Find the event
echo 'Looking for event with the Summary: "Work Meeting"' . '<BR>' . "\n";
$eventList = $service->events->listEvents('primary');
$items = $eventList->getItems();
$eventID = 'none';
foreach($items as $ev) {
$summary = $ev->getSummary();
if ($summary == "Work Meeting") {
$eventID = $ev->getId();
}
}

//Update the description
if ($eventID != 'none') {
$event = $service->events->get('primary', $eventID);

$event->setDescription('This is serious work');

$updatedEvent = $service->events->update('primary', $eventID, $event);

// Print the updated date.
echo 'The event was found and updated on: ' . $updatedEvent->getUpdated();
}
else {
echo 'Event not found!';
}

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

I hope someone finds this helpful.

Rudy Preston

unread,
Aug 25, 2013, 11:19:36 AM8/25/13
to google-api-php-client

Thanks for working for google for free.

--
You received this message because you are subscribed to the Google Groups "google-api-php-client" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-api-php-c...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages