【问题标题】:Fatal error: Call to a member function insert() on a non-object致命错误:在非对象上调用成员函数 insert()
【发布时间】:2014-03-14 19:24:43
【问题描述】:

我正在尝试直接从 php 脚本将事件添加到 Google 日历,但出现此错误:

致命错误:在非对象上调用成员函数 insert()...

<?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;

?>

我在许多我看过的示例中看到了一些使用类似于此的代码:

$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);

但这在我看来好像有一些应用程序正在被引用并且是生成日历事件的原因。就我而言,理想情况下,我只希望我的 php 脚本创建日历条目。不涉及其他“应用程序”。

我是否需要拥有 Google_Client 才能将简单条目添加到 Google 日历?对我来说这似乎太过分了,但也许这是唯一的方法。

我是否忽略了这个过程中的一个步骤?或者我写的代码中是否存在错误。任何帮助表示赞赏,包括示例链接。谢谢。

【问题讨论】:

    标签: php google-api-php-client


    【解决方案1】:

    您必须在 Google Developer's Console 上注册您的应用程序,如果您要使用 new official PHP Client Library,则必须使用 Google_Client() 类并设置所有这些值才能访问 API 服务。

    我建议你从这里开始:Write your First App

    这些页面上的示例代码使用旧的 PHP 库,但您可以了解应该做什么并将它们转换为新的库(大部分只是使用新的类名 - 检查源代码)。

    【讨论】:

    • 谢谢,@Vinicius Pinto。我正在探索的是“应用程序”的定义。我在 Google Play 上有移动应用程序,但这是不同的。我只是在编写一个服务器端脚本来将事件放到日历中,然后订阅日历的人会从中看到。我的小脚本是否构成我需要注册的“应用程序”?再次感谢!
    • 是的,任何访问 API 的程序都被视为应用程序,包括您的小脚本。
    【解决方案2】:

    你正在尝试这样做:

    $cal->events->insert
    

    但是我可以告诉你,你没有一个名为$cal 的对象。创建一个符合您要执行的操作的 $cal 的新实例,然后执行插入。

    【讨论】:

    • 谢谢。你完全正确。我省略了构建 $cal 对象的代码行。我的问题表述得不是很好。我想知道是否可以在没有 Google_Client 的情况下向 Google 日历添加条目(即绕过它)。也许不是,但 Google_Client 似乎只是添加一个日历条目有点过分。我将改写问题以使其更清楚。谢谢!
    • 我以前没有使用过 Google API,但如果我重新开始,我会像使用任何其他 API 一样使用 cURL。这是一个SO page,关于如何做到这一点。可能还有更多。希望有帮助!
    猜你喜欢
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 2013-11-25
    • 2012-05-30
    • 2016-08-30
    • 2015-02-06
    • 2012-05-14
    • 2013-11-19
    相关资源
    最近更新 更多