使用 Google 日历 API (PHP) 插入包含非 ASCII 字符的事件


Inserting events that contain non-ASCII characters using the Google Calendar API (PHP)

我在使用 Google 日历 API (PHP( 的 v3 插入事件时遇到问题。

如果事件的说明包含井号 £ 等字符,则会在日历中创建该事件,但说明留空。似乎对于初始 7 位字符代码(ASCII 代码 0-127(之外的所有字符都是如此。

通过使用htmlentities函数,我能够将井号的所有实例替换为:£

如果用户使用的是基于网络的 Google 日历版本,但移动应用不会将其转换回井号,则这很好。

这是一个相当大的问题,因为事件通常是从使用非 ascii 引号的 Word 复制/粘贴Microsoft。

有没有某种编码方法可以解决这个问题?我目前正在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。

事实证明,有一个简单的解决方案。我将HTTP标头设置为使用utf-8字符集,但没有专门编码我的描述。要将我的描述添加到我现在使用的事件中,请执行以下操作:

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