2015-05-26 98 views
1

我试图通过使用电子邮件发送简单的推送通知与pushbullet只是通过使用电子邮件并发送到链接的帐户,以避免需要帐户数据。 (见参考这里:https://docs.pushbullet.com/#pushes在PHP中发送POST请求pushbullet API 401错误

因此我使用PHP中的非卷曲的方法,我(不仅)在这里找到: How do I send a POST request with PHP?

不幸的是我回来的错误如下:

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

将file_get_contents使用url的选项设置为“on”。

我的代码:

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

编辑:代码改为christopherhesse的回应,仍然无法正常工作。它也不应该要求访问令牌,因为我知道推送。我理解它是将通知从中性推送到链接的电子邮件。也许我错了,但访问令牌不能解决它。

编辑(解决):访问令牌IS需要推送通知,因为它不能用这种方法,它可以使用cURL。

回答

0

您需要为此使用cURL,以便您可以将API密钥作为文档中定义的标头传递: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); 

此代码全部为我的头的顶部,并没有经过测试

我已经折断的选项,以便您可以很容易地看到他们,但你可以将它们组合成一个阵列和通过他们curl_setoptarray($curl, []);

+0

这样做,谢谢。 – Pinetrax

+0

你非常欢迎,很高兴帮助。 – nblackburn

1

听起来就像您缺少用户帐户的访问令牌。你可以在https://www.pushbullet.com/account找到它。您应该将其包含在“授权”这样的标头中:“承载者ACCESS_TOKEN_HERE”。

+0

遗憾的是没有做的伎俩,但我也觉得不该我不需要,我相应地更新了代码并发表了一条小评论。 – Pinetrax

+0

你肯定需要成为带有访问令牌的Pushbullet用户才能调用/ v2 /推送。你的标题应该可能更类似于: 'header'=> [“Content-type:application/x-www-form-urlencoded”,“Authorization:Bearer ”], –

+0

是的,我坚决需要一个,在那里确实错了。它仍然不起作用,但它确实如此。 – Pinetrax

1

我知道这是一个旧的帖子,但我与sendgrid使用相同的授权:承载APIKEY方法作为您的网站相同的问题。

我终于得到它使用原始的PHP后使用下面的代码

$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"; 

好像切换头的顺序,而忽略了\ r \ n在年底似乎工作上班。我花了一个小时在这尝试不同的组合,所以我想我会发布给其他人。

注意,如果您使用sendgrid API请务必设置“内容类型:应用程序/ JSON”,并确保你的$ post_data是JSON