Giuseppe Parrello

 

PHP - How to send a Pushbullet push


In this page I provide you with PHP scripts in order to send a Pushbullet's push. Please take note of the following requirements:

For further details about Pushbullet's API, refer to this page.


Sending a push "Note"

To execute this script, please take note of the following requirements:

<?php

$token = '[token]';
$body  = "My body";
$title = "My Title";

$ch = curl_init();

$postdata = json_encode([
             "type" => "note",
             "body" => $body,
             "title" => $title,
             ]);

$header = array();
$header[] = "Access-Token: " . $token;
$header[] = "Content-Type: application/json";

curl_setopt($ch,CURLOPT_URL, "https://api.pushbullet.com/v2/pushes");
curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
 
$result = curl_exec($ch);

print_r($result);
print_r("\r\n\r\n");

curl_close($ch);
?>

 

Sending a push "Link"

To execute this script, please take note of the following requirements:

<?php

$token = '[token]';
$body  = "My body";
$title = "My Title";
$link  = "http://www.google.com";

$ch = curl_init();

$postdata = json_encode([
             "type" => "link",
             "body" => $body,
             "title" => $title,
             "url"  => $link,
             ]);

$header = array();
$header[] = "Access-Token: " . $token;
$header[] = "Content-Type: application/json";

curl_setopt($ch,CURLOPT_URL, "https://api.pushbullet.com/v2/pushes");
curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
 
$result = curl_exec($ch);

print_r($result);
print_r("\r\n\r\n");

curl_close($ch);
?>

 

Sending a push "File"

To send a file, there are two ways. In the first way, we must have a file already available. In the second way, we have to retrieve a file from Internet.
The following script, retrieves a file already in the same folder of the script. To execute this script, please take note of the following requirements:

<?php

$token  = '[token]';
$fname  = "picture.jpg";
$body   = "My body";
$title  = "My Title";

$ftype = mime_content_type($fname);

$ch = curl_init();

$header = array();
$header[] = "Access-Token: " . $token;
$header[] = "Content-Type: application/json";

$postdata = json_encode([
             "file_name" => $fname,
             "file_type" => $ftype,
             ]);

curl_setopt($ch,CURLOPT_URL, "https://api.pushbullet.com/v2/upload-request");
curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
 
$result = curl_exec($ch);
curl_close($ch);

$obj = json_decode($result);
$file_name  = $obj->{'file_name'};
$file_url   = $obj->{'file_url'};
$file_type  = $obj->{'file_type'};
$upload_url = $obj->{'upload_url'};

$ch = curl_init();

$data['file'] = new CurlFile(realpath($fname), $ftype);

curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);
curl_close($ch);

$postdata2 = json_encode([
             "type" => "file",
             "body" => $body,
             "title" => $title,
             "file_name" => $file_name,
             "file_type" => $file_type,
             "file_url"  => $file_url,
             ]);

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, "https://api.pushbullet.com/v2/pushes");
curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata2);
curl_setopt($ch,CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
 
$result = curl_exec($ch);

curl_close($ch);
?>

The following script, retrieves a file from Internet. To execute this script, please take note of the following requirements:

<?php

$token  = '[token]';
$body   = "My body";
$title  = "My Title";
$fname  = "pexels.jpg";
$furl   = "https://images.pexels.com/photos/1681148/pexels-photo-1681148.jpeg";
$ftype  = "image/jpeg";

$header = array();
$header[] = "Access-Token: " . $token;
$header[] = "Content-Type: application/json";

$postdata = json_encode([
             "type" => "file",
             "body" => $body,
             "title" => $title,
             "file_name" => $fname,
             "file_type" => $ftype,
             "file_url"  => $furl,
             ]);

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, "https://api.pushbullet.com/v2/pushes");
curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch,CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
 
$result = curl_exec($ch);

print_r($result);
print_r("\r\n\r\n");

curl_close($ch);
?>