简体   繁体   English

试图使用PHP从日历中获取事件列表

[英]trying to get a list of events from a calendar using PHP

I am trying to get a list of events from a calendar. 我正在尝试从日历中获取事件列表。 i am looking at developers.google.com/google-apps/calendar/v3/reference/calendarList/list 我正在查看developers.google.com/google-apps/calendar/v3/reference/calendarList/list

I assume that $service is created from $cal = new apiCalendarService($client); 我假设$ service是从$ cal = new apiCalendarService($ client);创建的; The doc is not very specific. 该文档不是很具体。

when i try to execute the code below , i get the error PHP Fatal error: Call to a member function getItems() on a non-object 当我尝试执行以下代码时,出现错误PHP致命错误:在非对象上调用成员函数getItems()

but if i do this it works 但是如果我这样做的话

$calendarList = $cal->calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calendarList, true) . "</pre>";

Also if i try the example code @ 另外,如果我尝试示例代码@

developers.google.com/google-apps/calendar/v3/reference/events/list#examples I get the same kind of error PHP Fatal error: Call to a member function getItems() on a non-object developers.google.com/google-apps/calendar/v3/reference/events/list#examples我遇到了相同类型的错误PHP致命错误:在非对象上调用成员函数getItems()

This works: 这有效:

$events = $cal->events->listEvents('rtparies@gmail.com');    print "<h1>Events</h1><pre>" . print_r($events, true) . "</pre>";[/PHP]

This fails: 这将失败:

foreach($events->getItems()as $event){
echo $event->getSummary();

} }

I can not imagine all these examples are bad, so any suggestions on what i am doing wrong? 我无法想象所有这些例子都是不好的,所以关于我做错什么的任何建议?

The example is below : 示例如下:

$calendarList = $service->calendarList->listCalendarList();
while(true){
  foreach($calendarList->getItems()as $calendarListEntry){
      echo $calendarListEntry->getSummary();
   }
  $pageToken = $calendarList->getNextPageToken();
  if($pageToken){
      $optParams = array('pageToken'=> $pageToken);
      $calendarList = $service->calendarList->listCalendarList($optParams);
  }else{
      break;
  }
}

Thanks for any help 谢谢你的帮助

You need to get $calendarList as an Object, not Array as your current code does. 您需要获取$calendarList作为对象,而不是像当前代码那样获取Array。

Use 采用

$client->setUseObjects(true); 

just before 就在之前

$service = new apiCalendarService($client);

Explanation: 说明:

$events = $cal->events->listEvents('rtparies@gmail.com');

returns an Array. 返回一个数组。 This would be OK, if you wouldn't need an object for further processing. 如果您不需要对象进行进一步处理,那就可以了。

Since you need an object, you need to set the use of objects to true: 由于需要一个对象,因此需要将对象的使用设置为true:

$client->setUseObjects(true);

Now, 现在,

$events = $cal->events->listEvents('rtparies@gmail.com');

is still working, only the difference is that you now have an object, which you can use for your code below this line. 仍在工作,唯一的区别是您现在有了一个对象,您可以将其用于此行下方的代码。

try to update your code: 尝试更新您的代码:

     foreach($calendarList->getItems()as $calendarListEntry){
    echo '<h1>event->getSummary()='.$event->getSummary().'</h1>';
    $client->setUseObjects(true);   
    $events = $cal->events->listEvents($event->id);
    echo 'events=<pre>'; print_r($events); echo '</pre>';       
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM