1

I'm trying to send simple push notifications with pushbullet just through using the email and sending those to the linked account to avoid needing account data. (see reference here: https://docs.pushbullet.com/#pushes)

Therefore I'm using a non cURL-method in php that I (not only) found here: How do I send a POST request with PHP?

Unfortunately I get back an error as following:

<br />
<b>Warning</b>:  file_get_contents(https://api.pushbullet.com/v2/pushes): failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
 in <b>/path/to/function.php</b> on line <b>42</b><br />
bool(false)

Option to use urls for file_get_contents is set to "on".

My code:

$pushdata = array(
    "email"     => $email,
    "type"      => "link",
    "title"     => "Demo Pushbullet Notification",
    "body"      => "You have new comment(s)!",
    "url"       => "http://demo.example.com/comments"
);

//Post without cURL

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n"."Authorization: Bearer <xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>\r\n",
        'method'  => 'POST',
        'content' => http_build_query($pushdata),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents("https://api.pushbullet.com/v2/pushes", false, $context, -1, 40000);
var_dump($result);

EDIT: Altered the code to christopherhesse's response, still doesn't work. It also shouldn't require access-tokens as I understand that pushing. I understand it as pushing notification from neutral to an linked email. Maybe I'm wrong, but the access-token doesn't fix it.

EDIT(solved): An access-token IS needed to push notifications and as it doesn't work with this method, it does work with cURL.

Community
  • 1
  • 1
Pinetrax
  • 35
  • 1
  • 6

3 Answers3

1

Sounds like you're missing the access token for your user account. You can find it on https://www.pushbullet.com/account . You should include it in a header like 'Authorization': 'Bearer ACCESS_TOKEN_HERE'.

Chris Pushbullet
  • 1,039
  • 9
  • 10
  • Unfortunately doesn't do the trick but I also think it shouldn't be needed, I updated the code accordingly and a small comment. – Pinetrax May 27 '15 at 00:05
  • You definitely need to be a Pushbullet user with an access token in order to call /v2/pushes. Your headers should probably be something more like: 'header' => ["Content-type: application/x-www-form-urlencoded", "Authorization: Bearer "], – Chris Pushbullet May 27 '15 at 01:17
  • Yep, I defenetely need one, got really wrong there. It still didn't work, but with cURL it does. – Pinetrax May 27 '15 at 09:22
1

You need to be using cURL for this so you can pass the API key as a header defined in the documentation: https://docs.pushbullet.com/#http.

<?php

$curl = curl_init('https://api.pushbullet.com/v2/pushes');

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer <your_access_token_here>']);
curl_setopt($curl, CURLOPT_POSTFIELDS, ["email" => $email, "type" => "link", "title" => "Demo Pushbullet Notification", "body" => "You have new comment(s)!", "url" => "http://demo.example.com/comments"]);

// UN-COMMENT TO BYPASS THE SSL VERIFICATION IF YOU DON'T HAVE THE CERT BUNDLE (NOT RECOMMENDED).
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($curl);

print_r($response);

THIS CODE IS COMPLETELY OFF THE TOP OF MY HEAD AND HASN'T BEEN TESTED

I have broken the options up so you can see them easily but you can combine them into an array and pass them via curl_setoptarray($curl, []);

nblackburn
  • 268
  • 2
  • 14
1

I know this is an old post but I was having the same problems with sendgrid that uses the same Authorization: Bearer APIKEY method as your website.

I finally got it to work using raw php post using the following code

$url = "your url";
$post_data = 'yourdata';

// Set the headers
$options = array('http' =>
    array(
        'method'  => 'POST',
        'header' => "Authorization: Bearer APIKEY\r\n" . 'Content-Type: application/x-www-form-urlencoded',
        'content' => $post_data
    )
);

// Send the POST data
$ctx = stream_context_create($options);
$fp = fopen($url, 'rb', false, $ctx);
// Read the POST data
$result = json_decode(stream_get_contents($fp));
echo "done";

It seems like switching the order of the header and leaving out the \r\n at the end seemed to work. I spent like an hour on this trying different combinations so I thought I would post this for others.

Note if your using the sendgrid api make sure to set the 'Content-Type: application/json' and make sure your $post_data is in json

ZMH Tech
  • 23
  • 8