批量请求Google Calendar php API [英] Batch request Google Calendar php API

查看:123
本文介绍了批量请求Google Calendar php API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  $ client = new Google_Client(); 
$ plus =新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时,我变成了以下代码:
$ client = new Google_Client();
$ this-> service = new Google_CalendarService($ client);

  $ client-> setUseBatch(true) ; 
//创建新批次并填写2个事件
$ batch = new Google_BatchRequest();

$ gEvent1 =新的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();

但是,当我运行此代码时,出现此错误:

 可捕捉的致命错误:传递给Google_BatchRequest :: add()的参数1 b $ b必须是Google_HttpRequest的实例,Google_Event的实例给出

我不认为$ plus-> people-> get('')是一个HttpRequest。

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

预先感谢!

解决方案

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



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

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

这是我对insert方法的修改(您需要在日历服务中使用该方法输入的正确对象)。只需几行即可检查从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;
}
}


I'm working on a google Calendar sync with my application. I'm using the latest google-api-php-client

Now I want to update all my event, so i want to use the batch operation. The example code of the php client api is:

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

So when I "translate" it to the calendar API, I become the following code: $client = new 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();

But when I run this code, I get this error:

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

And I do not think that "$plus->people->get('')" is a HttpRequest.

Does anybody know what I do wrong, or what method / object I should use to add in the batch? Or what the correct use of the batch operation for the calendar is?

Thanks in advance!

解决方案

I had the same problem while working with inserts to the MirrorService api, specifically with timeline items. What is happening is that the the Google_ServiceRequest object is seeing that you've set the useBatch flag on the client and is actually returning returning Google_HttpRequest object before executing the call to Google but the insert statement in the calendar service doesn't properly handle it as such and ends up returning the calendar event object instead.

It also looks like your params to batch->add are backwards. Should be:

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

Here is my modification to the insert method (you'll need to do this in the calendar service with the proper object input to the method). Just a few lines to make it check what class is coming back from the ServiceRequest class:

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

这篇关于批量请求Google Calendar php API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆