Create event with PHP

86 views
Skip to first unread message

Anders Pedersen

unread,
Feb 16, 2012, 9:02:34 AM2/16/12
to Google Calendar API
Hey everyone, I'm fiddling with Google Calendar API, trying to make a
site where you as a member can join events.
My problem is to create an event from this site. The user is writing
all the values in a form which is being sent to a page that puts these
values into this:

<?php
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;
}
$summary=$_GET["summary"];
$location=$_GET["location"];
$description=$_GET["description"];
$starttime=$_GET["start"];
$attendeename=$_GET["attendeename"];
$attendeeemail=$_GET["attendeeemail"];
$calid=$_GET["calid"];


/*

*/
?>
<html>
<head>
<title>Kalender</title>


</head>
<body>
<?php
echo $calid;
$event = new Event();

$event->setSummary($summary);
$event->setDescription($description);
$event->setLocation($location);
$organizer = new EventOrganizer();
$organizer ->setEmail($attendeeemail);
$event->setOrganizer($organizer);
$event->setLocation($location);
$start = new EventDateTime();
$start->setDateTime($starttime);
$event->setStart($start);
$end = new EventDateTime();
$end->setDate('2012-16-02');
$event->setEnd($end);

$attendee1 = new EventAttendee();

$attendee1->setEmail($attendeeemail);
$attendee1->setDisplayName($attendeename);

// ...
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;
print_r('<pre>');
print_r($event);
print_r('</pre>');
$createEvent = $service->events->insert($calid, $event);

echo $createEvent->getId(); ?>

</body>
</html>
But when I do so, I get the following error:

Fatal error: Uncaught exception 'apiServiceException' with message
'Error calling POST
https://www.googleapis.com/calendar/v3/calendars/4u2l2652jno7n...@group.calendar.google.com/events?key=MYKEY:
(400) Bad Request' in /usr/home/anped7/public_html/kalender/src/io/
apiREST.php:86 Stack trace: #0 /usr/home/anped7/public_html/kalender/
src/io/apiREST.php(56):
apiREST::decodeHttpResponse(Object(apiHttpRequest)) #1 /public_html/
kalender/src/service/apiServiceResource.php(186):
apiREST::execute(Object(apiServiceRequest)) #2 /public_html/kalender/
src/contrib/apiCalendarService.php(493): apiServiceResource-
>__call('insert', Array) #3
/public_html/kalender/create_BE.php(68): EventsServiceResource-
>insert('4u2l2652jno7n8p...', Object(Event)) #4 {main} thrown in /usr/
home/anped7/public_html/kalender/src/io/apiREST.php on line 86

Can anyone please help me?
Sincerely, Anders

Dan Holevoet

unread,
Feb 16, 2012, 1:14:58 PM2/16/12
to google-ca...@googlegroups.com
Hi,

I suspect the date format you are using for start and end times is incorrect. It should be like the following: 2011-06-03T10:25:00.000-07:00

Since your users are entering this value themselves, you may wish to convert between the format they enter and the above format (or document for your users how to enter the correct format).

Thanks,
Dan


--
You received this message because you are subscribed to the Google Groups "Google Calendar API" group.
To post to this group, send email to google-ca...@googlegroups.com.
To unsubscribe from this group, send email to google-calendar...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-calendar-api?hl=en.




--
Dan Holevoet
Google Developer Relations

Anders Pedersen

unread,
Feb 17, 2012, 3:23:04 AM2/17/12
to Google Calendar API
Thank you for the answer, but it doesn't seem to work.

I can delete events, so I got access to the calendar.

Ray Baxter

unread,
Feb 17, 2012, 4:35:16 PM2/17/12
to google-ca...@googlegroups.com
I believe that Dan is correct.

Your code is

$start = new EventDateTime();
$start->setDateTime($starttime);
$event->setStart($start);
$end = new EventDateTime();
$end->setDate('2012-16-02');
$event->setEnd($end);

You are saying that you initializing a DateTime, but you are only providing a Date.

Ray

On Fri, Feb 17, 2012 at 12:23 AM, Anders Pedersen <anderspe...@gmail.com> wrote:
Thank you for the answer, but it doesn't seem to work.

I can delete events, so I got access to the calendar.
Reply all
Reply to author
Forward
0 new messages