【问题标题】:Inserting events that contain non-ASCII characters using the Google Calendar API (PHP)使用 Google Calendar API (PHP) 插入包含非 ASCII 字符的事件
【发布时间】:2013-10-11 18:55:09
【问题描述】:

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

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

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

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

这是一个相当大的问题,因为事件通常是从使用非 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。

【问题讨论】:

  • 为什么在 htmlentities 函数中使用 'cp1252'?
  • 这是针对我在其他地方发现的类似问题提出的,但并未解决问题。我最初使用'utf-8'作为参数。
  • 你能粘贴插入请求的http请求的转储吗?

标签: php encoding calendar ascii google-calendar-api


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多