使用Google Calendar API(PHP)插入包含非ASCII字符的事件 [英] Inserting events that contain non-ASCII characters using the Google Calendar API (PHP)

查看:194
本文介绍了使用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函数,我能够用& pound;

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.

这是一个相当大的问题,因为事件经常从Microsoft使用非ASCII引号的字。

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));

这篇关于使用Google Calendar API(PHP)插入包含非ASCII字符的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆