Giuseppe Parrello

 

PHP - Come inviare un push di Pushbullet


In questa pagina fornisco gli script in PHP per inviare un push di Pushbullet. Si prega di prendere nota dei seguenti requisiti:

Per maggiori informazioni sulle API di Pushbullet, fare riferimento a questa pagina.


Inviare un push "Note"

Per eseguire questo script, si prega di prendere nota dei seguenti requisiti:

<?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);
?>

 

Inviare un push "Link"

Per eseguire questo script, si prega di prendere nota dei seguenti requisiti:

<?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);
?>

 

Inviare un push "File"

Per inviare un file, ci sono due modi. Nel primo modo, bisogna avere un file già disponibile. Nel secondo modo, si recupera un file da Internet.
Il seguente script, recupera un file già presente nella stessa cartella dello script. Per eseguire questo script, si prega di prendere nota dei seguenti requisiti:

<?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);
?>

Il seguente script, recupera un file da Internet. Per eseguire questo script, si prega di prendere nota dei seguenti requisiti:

<?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);
?>