0

I am trying to insert/update timeline cards in the Google Glass with an external PHP script. The eventual goal is for the script to be called from an Eclipse plugin every time a change is made, and update a Glass timeline card with new data from the plugin. Is this possible? For example, how would I insert a “Hello, World!” card to Google Glass from a PHP script running on my Apache server? I've searched Google and Stack Overflow but have yet to come across anything explaining how to do this. My Glass application uses the Mirror API.

Thank you in advance for your help!

EDIT: I would like to insert a simple timeline card to the Glass using my PHP script. According to the Google developers guide, the raw HTTP should look something like this:

POST /mirror/v1/timeline HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer {auth token}
Content-Type: application/json
Content-Length: 26

{ "text": "Hello world" }

How can I write the above code in PHP to allow me to insert a timeline card to the Glass?

Also, is it possible to make this POST if my Apache server and Glass app are running on different ports?

Will Stone
  • 15
  • 3
  • It isn't clear from your question where you're having the problem. Are you having problems getting the Eclipse plugin to send a message to your PHP server, or your PHP server adding a card to Glass. Can you update your question (and then comment to say you've updated it) to provide more details. – Prisoner Aug 11 '14 at 10:56
  • My apologies. I've removed the 'Eclipse-plugin' tag and edited my question. Let me know if it is still unclear. – Will Stone Aug 11 '14 at 18:29

1 Answers1

1

Here's a PHP function which uses the Google PHP API Client Library to insert a simple timeline item. It was copied from the official timeline insert reference docs.

/**
 * Insert a new timeline item in the user's glass with an optional
 * notification and attachment.
 *
 * @param Google_MirrorService $service Authorized Mirror service.
 * @param string $text timeline item's text.
 * @param string $contentType Optional attachment's content type (supported
 *                            content types are "image/*", "video/*"
 *                            and "audio/*").
 * @param string $attachment Optional attachment content.
 * @param string $notificationLevel Optional notification level,
 *                                  supported values are {@code null}
 *                                  and "AUDIO_ONLY".
 * @return Google_TimelineItem Inserted timeline item on success, otherwise.
 */
function insertTimelineItem($service, $text, $contentType, $attachment,
                            $notificationLevel) {
  try {
    $timelineItem = new Google_TimelineItem();
    $timelineItem->setText($text);
    if ($notificationlevel != null) {
      $notification = new Google_NotificationConfig();
      $notification->setLevel($notificationLevel);
      $timelineItem->setNotification($notification);
    }
    $optParams = array();
    if ($contentType != null && $attachment != null) {
      $optParams['data'] = $attachment;
      $optParams['mimeType'] = $contentType;
    }
    return $service->timeline->insert($timelineItem, $optParams);
  } catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
    return null;
  }
}

You need an initialized PHP client library for this (or any PHP code using the Mirror API) to work. If you're just getting started with PHP and the Mirror API, I recommend you try out the PHP quick start project.

mimming
  • 13,974
  • 3
  • 45
  • 74
  • Thanks for the reply. Is it possible to do this if my PHP script and Glassware app are running on different ports? – Will Stone Aug 12 '14 at 17:36
  • In this case, the PHP script is your Glassware app. Since it uses the Mirror API, Google handles all the shuffling of data, so you don't need to open any sockets to Glass at all. – mimming Aug 12 '14 at 22:18