-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_bullet.php
235 lines (209 loc) · 5.33 KB
/
push_bullet.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
require_once("config.php");
class PushBullet {
/*
* Used to get a list of devices, returns a JSON string
*
* Example usage
* $pushbullet = new PushBullet();
* $devices = json_decode($pushbullet->get_devices());
* $deviceID = $devices->devices[0]->id;
*
* @return string
*/
public function get_devices()
{
global $push_bullet_apikey;
if(is_null($push_bullet_apikey))
die('Error no API key');
else
{
$ch = curl_init('https://www.pushbullet.com/api/devices');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$push_bullet_apikey:");
return curl_exec($ch);
}
}
/*
* Used to push a list to the device specified by $deviceID, returns the JSON output of the API.
*
* Example Usage
* $pushbullet = new PushBullet();
* $devices = json_decode($pushbullet->get_devices());
* $deviceID = $devices->devices[0]->id;
* $title = 'Sample List';
* $list = array(
* 'item1',
* 'item2',
* 'item3'
* );
* $pushbullet->push_list($deviceID,$title,$list);
*
*
* @param int $deviceID
* @param string $title
* @param array $list
*
* @return string
*
*/
public function push_list($deviceID,$title,$list)
{
return $this->push($deviceID,'list',$title,$list);
}
/*
* Used to push a URL to the device specified by $deviceID, returns the JSON output of the API.
*
* Example Usage
* $pushbullet = new PushBullet();
* $devices = json_decode($pushbullet->get_devices());
* $deviceID = $devices->devices[0]->id;
* $title = 'Sample List';
* $url = 'http://reddit.com';
* $pushbullet->push_url($deviceID,$title,$url);
*
*
* @param int $deviceID
* @param string $title
* @param string $url
*
*
* @return string
*
*/
public function push_url($deviceID,$title,$url)
{
return $this->push($deviceID,'link',$title,$url);
}
/*
* Used to push a note to the device specified by $deviceID, returns the JSON output of the API.
*
* Example Usage
* $pushbullet = new PushBullet();
* $devices = json_decode($pushbullet->get_devices());
* $deviceID = $devices->devices[0]->id;
* $title = 'Sample Note';
* $note = 'Test note, this will hopefully be pushed to the device if everything happens correctly.';
* $pushbullet->push_note($deviceID,$title,$note);
*
*
* @param int $deviceID
* @param string $title
* @param string $note
*
*
* @return string
*
*/
public function push_note($deviceID,$title,$note)
{
return $this->push($deviceID,'note',$title,$note);
}
/*
* Used to push a note to the device specified by $deviceID, returns the JSON output of the API.
*
* Example Usage
* $pushbullet = new PushBullet();
* $devices = json_decode($pushbullet->get_devices());
* $deviceID = $devices->devices[0]->id;
* $title = 'Google Inc.';
* $address = '1600 Amphitheatre Pkwy Mountain View, CA 94043';
* $pushbullet->push_address($deviceID,$title,$address);
*
*
* @param int $deviceID
* @param string $title
* @param string $address
*
*
* @return string
*
*/
public function push_address($deviceID,$title,$address)
{
return $this->push($deviceID,'address',$title,$address);
}
/*
* Please ignore this, it is very messy and is the actual method for pushing.
*
*
*
*
*
*/
public function push($device_id,$type,$title,$data)
{
global $push_bullet_apikey;
if(is_null($push_bullet_apikey))
die('Error no API key');
switch($type)
{
case "note":{
// Gather data to push a note
$post_data = array(
'device_id' =>$device_id,
'type' =>$type,
'title' =>$title,
'body' =>$data
);
break;
}
case "address":{
// Gather data to push a address
$post_data = array(
'device_id' =>$device_id,
'type' =>$type,
'name' =>$title,
'address' =>$data
);
break;
}
case "list":{
// Gather data to push a list
$post_data = array(
'device_id' =>$device_id,
'type' =>$type,
'title' =>$title,
'items' =>''// Set up a default blank space, will add the params later.
);
// Loop the $items manually to create the list.
foreach($data as $item)
{
$post_data['items'] = $item.'&items='.$post_data['items'];
}
break;
}
case "link":{
// Gather data to push a link
$post_data = array(
'device_id' =>$device_id,
'type' =>$type,
'title' =>$title,
'url' =>$data
);
break;
}
default:{
// $type was incorrect
die('Error, no matching PUSH type found. <br>'.$type.' is not a known push type or is not supported.');
}
}
foreach($post_data as $key=>$value) {
// URL-ify the data to post
$post_data_string .= $key.'='.$value.'&';
}
// Remove the final & to avoid messing with any servers.
rtrim($post_data_string, '&');
// Curl the data using the apikey as auth and post data
$ch = curl_init('https://www.pushbullet.com/api/pushes');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$push_bullet_apikey:");
curl_setopt($ch, CURLOPT_POST, count($post_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data_string);
return curl_exec($ch);
}
}
$push = new PushBullet();
echo 'pushcallback('.$push->push($push_bullet_device_id,$_REQUEST['type'],$_REQUEST['title'],$_REQUEST['data']).');';