1

我正在处理Google日历与我的应用程序同步。 我使用最新的google-api-php-client批量请求Google日历php API

现在我想更新我所有的事件,所以我想使用批处理操作。 PHP客户端API的示例代码:

$client = new Google_Client(); 
$plus = new Google_PlusService($client); 

$client->setUseBatch(true); 

$batch = new Google_BatchRequest(); 
$batch->add($plus->people->get(''), 'key1'); 
$batch->add($plus->people->get('me'), 'key2'); 
$result = $batch->execute(); 

所以,当我“翻译”它的日历API,我成了下面的代码: $客户端=新Google_Client(); $ this-> service = new Google_CalendarService($ client);

$client->setUseBatch(true); 
// Make new batch and fill it with 2 events 
$batch = new Google_BatchRequest(); 

$gEvent1 = new Google_event(); 
$gEvent1->setSummary("Event 1"); 

$gEvent2 = new Google_event(); 
$gEvent2->setSummary("Event 2"); 

$batch->add($this->service->events->insert('primary', $gEvent1)); 
$batch->add($this->service->events->insert('primary', $gEvent2)); 

$result = $batch->execute(); 

但是当我运行此代码,我得到这个错误:

Catchable fatal error: Argument 1 passed to Google_BatchRequest::add() 
    must be an instance of Google_HttpRequest, instance of Google_Event given 

而且我不认为 “$加功能>以人为>获取( '')” 是一个HttpRequest的。

有人知道我做错了什么,或者我应该用什么方法/对象来添加批处理? 或日历的批处理操作的正确用法是什么?

在此先感谢!

回答

1

我在插入MirrorService api的时候遇到了同样的问题,特别是时间轴项目。现在发生的事情是,Google_ServiceRequest对象看到您已经在客户端上设置了useBatch标志,并且实际上在执行对Google的调用之前返回了Google_HttpRequest对象,但日历服务中的插入语句没有正确处理它这样并最终返回日历事件对象。

它也看起来像你的参数批量添加是向后。应该是:

$batch->add($this->service->events->insert($gEvent1, 'primary')); 

下面是我修改插入方法(你需要与对象输入正确的方法做这个日历服务)。只需几行即可查看ServiceRequest类返回的课程类别:

public function insert(google_TimelineItem $postBody, $optParams = array()) { 
    $params = array('postBody' => $postBody); 
    $params = array_merge($params, $optParams); 
    $data = $this->__call('insert', array($params)); 
    if ($this->useObjects()) { 
    if(get_class($data) == 'Google_HttpRequest'){ 
     return $data; 
    }else{ 
     return new google_TimelineItem($data); 
    } 
    } else { 
    return $data; 
    } 
}