Reputation: 71
Am new in google calendar API, Written below code for sending invitation using google API.
<?
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
$setsummary="setsummary";
$setLocation="setLocation";
$event = new Google_Event();
$event->setSummary($setsummary);
$event->setLocation($setLocation);
$start_date=date();
$start = new Google_EventDateTime();
$start->setDateTime($start_date);
$event->setStart($start);
$end_date=date();
$end = new Google_EventDateTime();
$end->setDateTime($end_date);
$event->setEnd($end);
$event->sendNotifications=true;
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail('[email protected]');
$attendee2 = new Google_EventAttendee();
$attendee2->setEmail('[email protected]');
$attendees = array($attendee1,$attendee2);
$event->attendees = $attendees;
$opt= array ("sendNotifications" => true);
$createdEvent = $cal->events->insert('********calendar id***********', $event, $opt);
?>
but unfortunately it is not sending invitation. what I have missed in this? I just want to simply send invitation to [email protected]
Upvotes: 7
Views: 10682
Reputation: 6046
I used the updated version of Google PHP Client lib. Because Google_Auth_AssertionCredentials has been removed from Google Lib.
One more thing, you have to use the Composer to Install the Google Library.
After creating a project on Google Console with OAuth. You have to Enable Google Calendar API.
OAuth must have redirect_url which is going to be used in Calendar Token Generation. After this, on Google API Dashboard you have to download OAuth 2.0 Client JSON data and keep that file as "credentials.json" in your below-given Code Folder.
Use the below code in Your PHP File and Run it from Terminal (CLI).
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// If you've used composer to include the library
require __DIR__ . '/../vendor/autoload.php';
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Calendar API Test');
$client->setScopes( ['https://www.googleapis.com/auth/calendar'] );
// $client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// and refresh tokens, and is created automatically when the authorization flow completes for the first time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
// Check Param on redirected URL, for ?code=#############
// you have to copy only ?code= $_GET parms data and paste in console
$authCode = trim(fgets(STDIN)); // Get code after Authentication
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
$client = getClient();
$accessToken = $client->getAccessToken();
$service = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event(array(
'summary' => 'Some Text for summary',
'location' => '800 Howard St., San Francisco, CA 94103',
'description' => 'A chance to hear more about Your products.',
'start' => array(
'dateTime' => '2020-06-24T09:00:00',
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => '2020-06-24T10:00:00',
'timeZone' => 'America/Los_Angeles',
),
/*'recurrence' => array(
'RRULE:FREQ=DAILY;COUNT=2'
),*/
'attendees' => array(
array('email' => '[email protected]'),
array('email' => '[email protected]'),
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
array('method' => 'email', 'minutes' => 24 * 60),
array('method' => 'popup', 'minutes' => 10),
),
),
));
$opts = array('sendNotifications' => true, 'conferenceDataVersion' => true); // send Notification immediately by Mail or Stop Hangout Call Link
$event = $service->events->insert( 'primary', $event, $opts );
printf('Event created: %s\n', $event->htmlLink);
?>
It is working 100% for me.
Upvotes: 0
Reputation: 5831
Do Not use Zend, Use Google php client lib . Below code is fully tested https://github.com/google/google-api-php-client
Go to console and create a project
https://console.developers.google.com
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//if (!isset($_SESSION['access_token']) {
// session_start();
//}
// If you've used composer to include the library, remove the following line
// and make sure to follow the standard composer autoloading.
// https://getcomposer.org/doc/01-basic-usage.md#autoloading
require_once 'google-api-php-client/autoload.php';
$client_id = "client_id";
$client_email = 'client_email';
$private_key = file_get_contents('Project-e7659df9db10.p12');
$scopes = array('https://www.googleapis.com/auth/calendar');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
//print_r($client->getAuth());
$service = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Interview');
$event->setLocation('Dell');
$event->sendNotifications=true;
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2015-02-14T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2015-02-14T11:00:00.000-07:00');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('[email protected]');
// ...
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;
$sendNotifications = array('sendNotifications' => true);
$createdEvent = $service->events->insert('primary', $event, $sendNotifications);
echo $createdEvent->getId();
?>
Upvotes: 6