2016-10-10 83 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