Insert events containing non-ASCII characters using the Google Calendar API (PHP)

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

If the event description contains a symbol, such as the £ pound sign, the event is created on the calendar, but the description remains empty. This seems to apply to all characters outside the original 7-bit character codes (ASCII codes 0-127).

Using the htmlentities function, I can replace all instances of the pound sign: £

This is normal if the user uses the web version of Google Calendar, but mobile applications do not convert this back to the pound sign.

This is a pretty big problem since events are often copied and pasted from Microsoft Word, which use quotes other than ascii.

Is there a specific encoding method that will work around this? I am currently using UTF-8 encoding in a MySQL database and PHP scripts.

I use the following code to create an 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;
}

And this code inserts an event:

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

Sitting on this for quite some time, so any help is appreciated! My version of PHP is 5.5.4.

+4
source share
1 answer

It turns out that this is a simple solution. I would set an HTTP header to use utf-8 encoding, but did not specifically encode my description. To add my description to the event that I am currently using:

$event->setDescription(utf8_encode($description));
+1
source

All Articles