Skip to content

Instantly share code, notes, and snippets.

@EdwinHoksberg
Created November 15, 2017 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EdwinHoksberg/7286e7a0123b59f9ca80698bac324958 to your computer and use it in GitHub Desktop.
Save EdwinHoksberg/7286e7a0123b59f9ca80698bac324958 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