2013-10-10 47 views
4

我在使用Google Calendar API(PHP)的v3插入事件時遇到問題。使用Google Calendar API(PHP)插入包含非ASCII字符的事件

如果某個事件的描述包含諸如英鎊符號£之類的字符,則該事件將在日曆中創建,但描述爲空。看起來對於最初的7位字符代碼之外的所有字符(ASCII代碼0-127)都是如此。

通過使用htmlentities函數,我能夠與取代英鎊符號的所有實例:£

這是好的,如果用戶使用的是谷歌日曆,但移動應用的基於Web的版本不轉換此回到英鎊符號。

這是一個相當大的問題,因爲事件往往是從使用非ASCII字符的Microsoft Word複製/粘貼的。

有沒有一種編碼方法可以解決這個問題?我目前在MySQL數據庫和PHP腳本中使用UTF-8編碼。

我用下面的代碼來創建事件:

function buildGoogleEvent($title,$description,$start_time,$end_time,$location) { 
    // Create an event object and set some basic event information 
    $event = new Google_Event(); 
    $event->setSummary($title); 
    $event->setLocation($location); 
    $event->setDescription(htmlentities($description, ENT_NOQUOTES, 'utf-8')); 

    // Convert the start and end date/times to ATOM format 
    $start_time_atom = str_replace(" ", "T", $start_time); 
    $end_time_atom = str_replace(" ", "T", $end_time); 

    // Add the event start to the event object 
    $start = new Google_EventDateTime(); 
    $start->setDateTime($start_time_atom); 
    $start->setTimeZone('Europe/London'); 
    $event->setStart($start); 

    // Add the event end to the event object 
    $end = new Google_EventDateTime(); 
    $end->setDateTime($end_time_atom); 
    $end->setTimeZone('Europe/London'); 
    $event->setEnd($end); 

    return $event; 
} 

而這個代碼插入事件:

$createdEvent = $service->events->insert($google_calendar_id, $event); 

一直坐在這個相當長的一段時間,所以任何幫助表示讚賞!我的PHP版本是5.5.4。

+0

爲什麼在htmlentities函數中使用'cp1252'? –

+0

這是與我在其他地方發現的類似問題有關的建議,但並未解決問題。我最初使用'utf-8'作爲參數。 – JY707

+0

可以粘貼插入請求的http請求的轉儲。 – pinoyyid

回答

1

事實證明,這是一個簡單的解決方案。我將HTTP頭設置爲使用utf-8字符集,但沒有特別編碼我的描述。將我的描述添加到我正在使用的活動中:

$event->setDescription(utf8_encode($description));