简体   繁体   中英

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

I assume that $service is created from $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

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

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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