2016-10-10 39 views
0

我正在使用CodeIgniter构建具有Fullcalendar的应用程序,并试图集成Google日历。当从Google日历中检索事件时未呈现事件JSON编码

我无法使用JS并且首选使用PHP,所以我有一个名为Calendar.php的控制器调用Google Calendar API,获取主日历并列出事件。

编辑:这就是我现在越来越:

enter image description here

物体看起来像这样

GET http://localhost/admin/calendar/gcal...?start=2016-09-25&end=2016-11-06&_=1476324367596

200 OK 2.68s

响应

[{“title”:“lobna elatreby @nrc”,“type”:“gcal”,“calendar_id”:356,“notes”:“notes”,“start”:“2010-03-23T14 : 30:00-04:00“,”结束“:”2010-03-23T15:30:00-04:00“},{”title“:”生物测试1“,”类型“:”gcal“ calendar_id“ :356,”notes“:”notes“,”start“:”2013-02-08T18:30:00-05:00“,”end“:”2013-02-08T19:30:00-05 :

我用json_encode 00" }]([ '事件'=> $ eventsArr]),我得到了同样的错误

弃用警告。力矩()区已过时,使用力矩()UTCOFFSET代替。 参数:2010-03-23T14:30:00-04:00

弃用/ fullcalendar/moment.min.js:309:98 ...............

类型错误:t.start未定义

函数C(t)的{空== t.allDay & &(t.allDay =(t.start.hasTime()|| t.end & &! t.end.hasTime ...

我试过了下载&更新了Moment.js,我试过了更改日期格式... Google Calender API “时间,作为组合的日期时间值(根据RFC3339格式化)。除非在timeZone中明确指定时区,否则需要时区偏移量。“

+0

仅复制的请求响应,并验证字符串是否使用(JSON格式)一个有效的JSON {https://jsonformatter.curiousconcept.com/(例如)。如果json无效,则不会呈现事件。要回复此消息,请务必在评论中添加@milz。 –

+0

@milz你是绝对正确的,我试图修复日期格式,但codeigniter日期助手不会格式化为RFC-4627。它将格式化为ISO-8601,HTTP Cookies,Atom和一些使用jsonformatter的RFC格式,但显然不起作用。有没有办法使用标准的PHP转换日期?谢谢 – nivanmorgan

+0

请复制json字符串并将其发布到您的问题而不是图像上。如果没有完整的json字符串,我无法测试它。谢谢。 –

回答

0

您从服务器返回的内容不是有效的JSON,问题与日期无关。看你已经发布的截图中,有这样的:。

{"events": {"description":"lobna elatreby @nrc", "start": "2010-03-23T14:30:00-04:00", "end": "2010-03-23T15:30:00-04:00"}}{"events":...

你设置多个events,这是因为在gcalendar_events函数的代码,您需要更新的代码(取看方法的结尾:

function gcalendar_events() { 
    $client = $this->getClient(); 

    $client->addScope(Google_Service_Calendar::CALENDAR); 
    // $client->setRedirectUri(site_url('calendar/gcalendar')); 
    //$client->setAccessType('offline'); need calendar events to appear even if not logged in to google 
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 
     $client->setAccessToken($_SESSION['access_token']); 
     $access_token = $_SESSION['access_token']; 

     $service = new Google_Service_Calendar($client); 
     $id = 'primary'; 
     $calendar = new Google_Service_Calendar_Calendar(); 
     $calendar = $service->calendars->get('primary'); 

     $event = new Google_Service_Calendar_Event(); 

     $events = $service->events->listEvents($id); 

     $eventsArr = []; 

     foreach ($events->getItems() as $event) {   
      // Append a new evet to the $eventsArr 
      $eventsArr[] = array() 
       'description' => $event->getSummary(),    
       'start'=> $event->getStart()->dateTime,     
       'end' => $event->getEnd()->dateTime, 
      ); 
     } 

     // Return a single `events` with all the `$eventsArr` 
     echo json_encode(['events' => $eventsArr]); 
    } 
} 

这应该返回类似:

{"events": [{"description":"lobna elatreby @nrc", "start": "2010-03-23T14:30:00-04:00", "end": "2010-03-23T15:30:00-04:00"}, {"description": "...", "start": ...}]

+0

我看着萤火虫调试器中的DOM窗口对象构造函数,并且dateformatter是红色的......这是否意味着日期中有错误呢?我不知道该怎么做日期..我的ical和其他日历显示这种格式Ymd H:我:S @milz – nivanmorgan

+0

@dr_neevmorgan我不认为这个问题与日期有关,因为momentjs可以处理许多不同的格式。此外,萤火虫可能会告诉错误是在某处,但问题出现在另一个地方。我建议你按照我在答案中描述的方法更新方法,如果它仍然不起作用,请从响应中复制整个字符串(json对象),然后用该字符串编辑你的问题。这是我能找出问题的唯一途径。谢谢! –

+0

好的,谢谢@milz – nivanmorgan

相关问题