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

我在使用 Google Calendar API (PHP) v3 插入事件时遇到问题.

I'm having trouble inserting events using v3 of the Google Calendar API (PHP).

如果某个事件的描述包含诸如井号 £ 之类的字符,则会在日历中创建该事件,但该描述将留空.似乎对于初始 7 位字符代码(ASCII 代码 0-127)之外的所有字符都是如此.

If a description for an event contains a character such as the pound sign £, the event is created in the calendar but the description is left blank. It seems that this is true of all characters outside of the initial 7-bit character codes (ASCII codes 0-127).

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

By using the htmlentities function I am able to replace all instances of the pound sign with: £

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

This is fine if the user is using a web-based version of Google Calendar but mobile apps do not convert this back to the pound sign.

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

This is quite a big issue as events are often copy/pasted from Microsoft Word which uses non-ascii quotation marks.

是否有某种编码方法可以解决这个问题?我目前在 MySQL 数据库和 PHP 脚本中使用 UTF-8 编码.

Is there a certain method of encoding that will get around this? I'm currently using UTF-8 encoding in the MySQL database and PHP scripts.

我正在使用以下代码来创建事件:

I'm using the following code to create the event:

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.

Been sitting on this for quite a while so any help is appreciated! My PHP version is 5.5.4.

推荐答案

原来有一个简单的解决方案.我将 HTTP 标头设置为使用 utf-8 字符集,但没有专门对我的描述进行编码.要将我的描述添加到我正在使用的事件中:

It turns out there's an easy solution to this. I'd set the HTTP header to use the utf-8 charset but hadn't specifically encoded my description. To add my description to the event I'm now using:

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

相关文章