Вопрос

I'm trying to add an event to a Google calendar directly from a php script but am getting this error:

Fatal error: Call to a member function insert() on a non-object...

<?php

set_include_path("scripts/google-api-php-client/src/" . PATH_SEPARATOR . get_include_path());

$path = $_SERVER['DOCUMENT_ROOT'];
$google_client = $path . '/xxxx/scripts/google-api-php-client/src/Google_Client.php';
include ($google_client);

require_once $path . '/xxxx/scripts/google-api-php-client/src/contrib/Google_CalendarService.php';

$event = new Google_Event();
$event->setSummary('Pi Day');
$event->setLocation('Math Classroom');
$start = new Google_EventDateTime();
$start->setDateTime('2013-03-14T10:00:00.000-05:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-03-14T10:25:00.000-05:00');
$event->setEnd($end);

// error is on this next line
$createdEvent = $cal->events->insert('some_calendar@gmail.com', $event);

echo $createdEvent->id;

?>

I've seen in many of the examples that I have looked at that some use code similar to this:

$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Event Creator");
$client->setClientId('MY CLIENT ID ADDRESS IS HERE');
$client->setClientSecret('MY CLIENT SECRET KEY IS HERE');
$client->setRedirectUri('http://localhost/phpt/caladd.php');
$client->setDeveloperKey('MY API KEY IS HERE');
$cal = new Google_CalendarService($client);

But this looks to me like there is some application that is being referenced and is what is generating the calendar event. In my case, ideally, I just want my php script to make the calendar entry. There is no other "application" involved.

Do I need to have a Google_Client in order to add a simple entry to a Google Calendar? It seems excessive to me, but maybe that's the only way to do this.

Am I overlooking a step in this process? Or is there a bug in the code as I've written it. Any help is appreciated, including links to examples. Thank you.

Это было полезно?

Решение 2

You must register your application on the Google Developer's Console and if you're going to use the new official PHP Client Library, you'll have to use the Google_Client() class and set all those values in order to access the API services.

I suggest you start here: Write your First App

The sample codes on those pages use the old PHP library, but you can get an idea of what should be done and convert them to the new lib (mostly just use the new class names - check the source).

Другие советы

You are trying to do this:

$cal->events->insert

But you do not have an object named $cal that I can tell. Create a new instance of $cal that conforms to what you are trying to do and then perform the insert.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top