Reputation: 557
How can I insert an event into a google calendar?
I am using this guide: https://developers.google.com/google-apps/calendar/v3/reference/events/insert
In the examples section, there is this php code:
$event = new Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
echo $createdEvent->getId();
But that gives me a fatal error because $service is undefined. Can anybody tell me how to initialize $service and make this stuff work?
Upvotes: 4
Views: 35402
Reputation: 391
full code for calendar:
this session has all
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
//require_once '../../src/vendor/autoload.php';
session_start();
print_r($_SESSION);
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
// $client->setClientId('insert_your_oauth2_client_id');
// $client->setClientSecret('insert_your_oauth2_client_secret');
// $client->setRedirectUri('insert_your_oauth2_redirect_uri');
// $client->setDeveloperKey('insert_your_developer_key');
$cal = 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()) {
$calList = $cal->calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>
Upvotes: -1
Reputation: 19
My script worked well so, with insert, change and delete Google Calenda event
<!DOCTYPE html>
<!--
*********** GOOGLE CALENDAR ****************************
Author: Vanderlei Bailo
E-mail: [email protected]
Script: insert, change and delete Google Calenda event
-->
<html>
<head>
<title>GOOGLE CALENDAR - insert, change and delete Google Calenda event</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<style>
body{
margin: 0;
width: 100%;
font-family: Verdana, Arial;
}
#centro{
width: 780px;
margin: auto;
}
.calendario{
position: relative;
width: 800px;
height: 600px;
margin-left:-390px;
left: 50%;
float: left;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
#datahora{
width: 250px;
float: left;
}
#cento{
width: 780px;
float: left;
}
#centro .primo{
width: 100%;
background-color: #E3E9FF;
padding: 10px;
margin: 50px 0;
float: left;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
label {
width: 780px;
margin: 5px 5px 0;
float: left;
padding-top: 10px;
}
input{
margin: 5px;
float: left;
padding: 5px 10px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
border: 1px #CCC solid;
}
input[type="text"]{
width: 750px;
}
input[type="date"]{
width: 125px;
}
input[type="time"]{
width: 70px;
}
input[type="submit"]{
}
input:focus{
border: 1px #cc0000 solid;
box-shadow: 0 0 5px #cc0000;
}
.btn {
background: #3498db;
background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
background-image: -moz-linear-gradient(top, #3498db, #2980b9);
background-image: -ms-linear-gradient(top, #3498db, #2980b9);
background-image: -o-linear-gradient(top, #3498db, #2980b9);
background-image: linear-gradient(to bottom, #3498db, #2980b9);
-webkit-border-radius: 5;
-moz-border-radius: 5;
border-radius: 5px;
font-family: Arial;
color: #ffffff;
font-size: 20px;
padding: 10px 20px 10px 20px;
text-decoration: none;
cursor: pointer;
}
.btn:hover {
background: #3cb0fd;
background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db);
background-image: -moz-linear-gradient(top, #3cb0fd, #3498db);
background-image: -ms-linear-gradient(top, #3cb0fd, #3498db);
background-image: -o-linear-gradient(top, #3cb0fd, #3498db);
background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
text-decoration: none;
}
</style>
</head>
<body>
<?php
session_start();
require 'google-api-php-client-master/src/Google/autoload.php';
require_once 'google-api-php-client-master/src/Google/Client.php';
require_once 'google-api-php-client-master/src/Google/Service/Calendar.php';
$client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'; //change this
$Email_address = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.gserviceaccount.com'; //change this
$key_file_location = 'xxxxxxxxxxxxxxxxxxxxxxxx.p12'; //change this
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);
$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);
$calendarList = $service->calendarList->listCalendarList();
while (true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo "<a href='Oauth2.php?type=event&id=" . $calendarListEntry->id . " '>" . $calendarListEntry->getSummary() . "</a><br>\n";
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
if ($_POST) {
$Summary = $_POST['Summary'];
$Location = $_POST['Location'];
$DateStart = $_POST['DateStart'];
$TimeStart = $_POST['TimeStart'];
$DateEnd = $_POST['DateEnd'];
$TimeEnd = $_POST['TimeEnd'];
$acao = $_POST['acao'];
if ($acao == 'Inserire') {
//--------------- trying to insert EVENT ---------------
$event = new Google_Service_Calendar_Event();
$event->setSummary($Summary);
$event->setLocation($Location);
$start = new Google_Service_Calendar_EventDateTime();
$datatimeI = geratime(DataIT2DB($DateStart), $TimeStart);
$start->setDateTime($datatimeI);
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$datatimeF = geratime(DataIT2DB($DateEnd), $TimeEnd);
$end->setDateTime($datatimeF);
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('[email protected]');
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
$_SESSION['eventID'] = $createdEvent->getId();
} else if ($acao == 'Cancellare') {
//--------------- trying to del EVENT ---------------
$createdEvent = $service->events->delete('primary', $_SESSION['eventID']);
} else if ($acao == 'Aggiornare') {
//--------------- trying to update EVENT ---------------
$rule = $service->events->get('primary', $_SESSION['eventID']);
$event = new Google_Service_Calendar_Event();
$event->setSummary($Summary);
$event->setLocation($Location);
$start = new Google_Service_Calendar_EventDateTime();
$datatimeI = geratime(DataIT2DB($DateStart), $TimeStart);
$start->setDateTime($datatimeI);
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$datatimeF = geratime(DataIT2DB($DateEnd), $TimeEnd);
$end->setDateTime($datatimeF);
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('[email protected]'); //change this
$attendees = array($attendee1);
$event->attendees = $attendees;
$updatedRule = $service->events->update('primary', $rule->getId(), $event);
}
}
function DataIT2DB($datapega) {
if ($datapega) {
$data = explode('/', $datapega);
if (count($data) > 1) {
$datacerta = $data[2] . '-' . $data[1] . '-' . $data[0];
} else {
$datacerta = $datapega;
}
} else {
$datacerta = $datapega;
}
return $datacerta;
}
function geratime($DateStart, $TimeStart) {
$dataHora = $DateStart . 'T' . $TimeStart . ':00.000+02:00'; //Fuso Rome
return $dataHora;
}
?>
<div id="contenut" style="width: 100%; float: left;">
<div id="centro">
<div class="primo">
<form name="adicionar" method="POST" action="#">
ID evento: <?php echo ( isset($_SESSION['eventID']) ? $_SESSION['eventID'] : "" ); ?>
<input type="hidden" name="" value="<?php echo ( isset($_SESSION['eventID']) ? $_SESSION['eventID'] : "" ); ?>" />
<input type="text" name="Summary" value="<?php echo ( isset($_POST['Summary']) ? $_POST['Summary'] : "" ); ?>" placeholder="Titolo"/>
<input type="text" name="Location" value="<?php echo ( isset($_POST['Location']) ? $_POST['Location'] : "" ); ?>" placeholder="Località "/>
<div id="datahora">
<label>Data e ora di inizio</label>
<input type="date" name="DateStart" value="<?php echo ( isset($_POST['DateStart']) ? $_POST['DateStart'] : "" ); ?>" placeholder="GG/MM/AAAA"/>
<input type="time" name="TimeStart" value="<?php echo ( isset($_POST['TimeStart']) ? $_POST['TimeStart'] : "" ); ?>" placeholder="10:20"/>
</div>
<div id="datahora">
<label>Data e ora di fine</label>
<input type="date" name="DateEnd" value="<?php echo ( isset($_POST['DateEnd']) ? $_POST['DateEnd'] : "" ); ?>" placeholder="GG/MM/AAAA"/>
<input type="time" name="TimeEnd" value="<?php echo ( isset($_POST['TimeEnd']) ? $_POST['TimeEnd'] : "" ); ?>" placeholder="10:20" />
</div>
<div id="cento">
<input class="btn" type="submit" value="Inserire" name="acao" />
<input class="btn" type="submit" value="Cancellare" name="acao" />
<input class="btn" type="submit" value="Aggiornare" name="acao" />
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Upvotes: 1
Reputation: 31
In the newest version of Google API v3 PHP client you should use
$event = new Google_Event();
instead of
$event = new Event();
And also use
$start = new Google_EventDateTime();
instead of
$start = new EventDateTime();
And of course the require definitions are different, use this one:
require_once '../src/Google_Client.php';
require_once '../src/contrib/Google_CalendarService.php';
Upvotes: 3
Reputation: 8201
It's described on this page. Code is copied below.
src/config.php
global $apiConfig;
$apiConfig = array(
// Site name to show in Google's OAuth authentication screen
'site_name' => 'www.example.org',
// OAuth2 Setting, you can get these keys on the API Access tab on
// the Google APIs Console
'oauth2_client_id' => 'YOUR_CLIENT_ID',
'oauth2_client_secret' => 'YOUR_CLIENT_SECRET',
'oauth2_redirect_uri' => 'YOUR_REDIRECT_URL',
// The developer key; you get this from the Google APIs Console
'developer_key' => 'YOUR_DEVELOPER_KEY',
// Which Authentication, Storage and HTTP IO classes to use.
'authClass' => 'apiOAuth2',
// Definition of service specific values like scopes, OAuth token URLs, etc
'services' => array(
'calendar' => array('scope' => 'https://www.googleapis.com/auth/calendar'),
)
);
your script
session_start();
require_once "../src/apiClient.php";
require_once "../src/contrib/apiCalendarService.php";
$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);
if (isset($_SESSION['oauth_access_token'])) {
$apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
$token = $apiClient->authenticate();
$_SESSION['oauth_access_token'] = $token;
}
Upvotes: 0
Reputation: 21194
$service = new apiCalendarService($apiClient);
See https://developers.google.com/google-apps/calendar/instantiate (switch to PHP source on the top right) for more information
Upvotes: 2