forked from googleglass/mirror-quickstart-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror-client.php
executable file
·111 lines (103 loc) · 3.67 KB
/
mirror-client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
* Copyright (C) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Author: Jenny Murphy - http://google.com/+JennyMurphy
// Utility functions for interacting with the Mirror API
// You will probably have to modify and expand this set for your project.
// These were adapted from code samples on the Mirror API reference
// found at https://developers.google.com/glass/v1/reference
require_once 'config.php';
function insertTimelineItem($service, $timelineItem, $contentType, $attachment)
{
try {
$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 ocurred: ' . $e->getMessage();
return null;
}
}
/**
* Subscribe to notifications for the current user.
*
* @param Google_MirrorService $service Authorized Mirror service.
* @param string $collection Collection to subscribe to (supported
* values are "timeline" and "locations").
* @param string $userToken Opaque token used by the Service to
* identify the user the notification pings
* are sent for (recommended).
* @param string $callbackUrl URL receiving notification pings (must be HTTPS).
*/
function subscribeToNotifications($service, $collection, $userToken, $callbackUrl)
{
try {
$subscription = new Google_Subscription();
$subscription->setCollection($collection);
$subscription->setUserToken($userToken);
$subscription->setCallbackUrl($callbackUrl);
$service->subscriptions->insert($subscription);
return "Subscription inserted!";
} catch (Exception $e) {
return 'An error occurred: ' . $e->getMessage();
}
}
function insertContact($service, $contactId, $displayName, $iconUrl)
{
try {
$contact = new Google_Contact();
$contact->setId($contactId);
$contact->setDisplayName($displayName);
$contact->setImageUrls(array($iconUrl));
return $service->contacts->insert($contact);
} catch (Exception $e) {
print 'An error ocurred: ' . $e->getMessage();
return null;
}
}
/**
* Delete a contact for the current user.
*
* @param Google_MirrorService $service Authorized Mirror service.
* @param string $contactId ID of the Contact to delete.
*/
function deleteContact($service, $contactId) {
try {
$service->contacts->delete($contactId);
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
}
/**
* Download an attachment's content.
*
* @param string $timelineId ID of the timeline item the attachment belongs to.
* @param Google_Attachment $attachment Attachment's metadata.
* @return string The attachment's content if successful, null otherwise.
*/
function downloadAttachment($itemId, $attachment) {
$request = new Google_HttpRequest($attachment['contentUrl'], 'GET', null, null);
$httpRequest = Google_Client::$io->authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
// An error occurred.
return null;
}
}