I’m trying to import an event into my Google Calendar account, however my code fails to import due to an error: Cannot use object of type Event as array this appears in line 2149 in my script which is located here:
$exported_events = array();
foreach($events as $event_id => $event)
{
$event = new Event();
$event->setSummary($event->location);
$event->setLocation($event->location);
$start = new EventDateTime();
$start->setDateTime($event['starts']); // Line 2149
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime((object) $event['ends']);
$event->setEnd($end);
$attendees = array();
foreach($event['people'] as $i => $person)
{
$attendee = new EventAttendee();
$attendee->setEmail($person->email);
$attendees[] = $attendee;
$event->attendees = $attendees;
}
$createdEvent = $service->events->insert('primary', $event);
$calendar_event_id = $createdEvent->getId();
if(!is_null($calendar_event_id))
{
$exported_events[] = $event_id;
}
}
Now, as my foreach loop walks over the $events array, it threw an error with $event['location'] so I changed it to $event->location, now it throws an error on line 2149 which is an array item (the array is printed below the page) and if I change it to $event->starts it will throw me this error: Undefined property: Event::$starts even if I put (object) $event['starts'] it will still won’t work.
How can I pass it to make it work? var_dump($event['starts']) returns as a string.
The array of per each $event:
Array
(
[location] => Some location in the US
[starts] => 2012-03-13T10:00:00
[ends] => 2012-03-13T14:00:00
[people] => Array
(
[0] => Array
(
[name] => Joan Uton
[email] => rnewkj@hotmail.co.uk
)
[1] => Array
(
[name] => Jack Sparrow
[email] => potc@gmail.com
)
[2] => Array
(
[name] => Barack Obama
[email] => barak@obama.com
)
)
)
It seems as you are using two types of $event. One as an array and one as an object.
Refactor by giving one type a different name.