Skip to content

Instantly share code, notes, and snippets.

@sidouglas
Forked from EdwinHoksberg/pushbullet.php
Created January 8, 2020 10:50
Show Gist options
  • Save sidouglas/8be04f5b8ece430dd2c9e6953ded301f to your computer and use it in GitHub Desktop.
Save sidouglas/8be04f5b8ece430dd2c9e6953ded301f to your computer and use it in GitHub Desktop.
Pushbullet push function
<?php
function pushbullet(string $accessToken, string $title, string $body, string $type = 'note'): \stdClass
{
if (!in_array($type, ['note', 'url'])) {
throw new \Exception('Invalid pushbullet push type');
}
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://api.pushbullet.com/v2/pushes',
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_HTTPHEADER => [
"Access-Token: {$accessToken}",
'Content-Type: application/json',
],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(compact('title', 'body', 'type')),
]);
$result = curl_exec($curl);
$json = json_decode($result);
curl_close($curl);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('Invalid json returned from pushbullet api');
}
if (!empty($json->error)) {
throw new \Exception("Invalid api request: {$json->error->message}");
}
return $json;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment