Created
July 20, 2018 15:54
-
-
Save OliPassey/78951ac225f82b5b5d427e61f0313f9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Testing sending via pushbullet channels, rather than direct to devices. | |
$pb_api_key = "ADD-YOUR-PB-API-KEY"; | |
// Capture incoming data and try to detect JSON | |
if($json = json_decode(file_get_contents("php://input"), true)) { | |
print_r($json); | |
$data = $json; | |
} else if(isset($_GET["mode"]) && $_GET["mode"] == "test") { | |
//rewrite targets so we only send tests to oli and steven | |
$data = $_GET; | |
} else { | |
print_r($_POST); | |
$data = $_POST; | |
} | |
$channel = $_GET ['channel']; | |
switch ($channel) { | |
case 0: | |
$pbchannel = "pushbullet-channel-1"; | |
break; | |
case 1: | |
$pbchannel = "pushbullet-channel-2"; | |
break; | |
case 2: | |
$pbchannel = "pushbullet-channel-3"; | |
break; | |
case 3: | |
$pbchannel = "pushbullet-channel-4"; | |
break; | |
case 4: | |
$pbchannel = "pushbullet-channel-5"; | |
break; | |
default : | |
echo "Please provide a channel target"; | |
break; | |
} | |
$pbtitle = $data["title"]; | |
$pbmessage = $data["message"]; | |
// Send the useful stuff onto Pushbullet | |
echo "Sending Notification ...\n"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://api.pushbullet.com/v2/pushes"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"channel_tag": "'.$pbchannel.'", "type": "note", "title":"'.$pbtitle.'", "body": "'.$pbmessage.'"}'); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_USERPWD, $pb_api_key); | |
$headers = array(); | |
$headers[] = "Content-Type: application/json"; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
// Write to a log file for debug and inspection | |
} | |
curl_close ($ch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment