发送PHP中的POST请求到pushbullet API 401错误 [英] send POST request in PHP to pushbullet API 401 error

查看:125
本文介绍了发送PHP中的POST请求到pushbullet API 401错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试仅通过使用电子邮件通过pushbullet发送简单的推送通知,然后将其发送到链接的帐户,以避免需要帐户数据.(请参见参考资料: https://docs.pushbullet.com/#pushes )

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)

因此,我在php中使用了一种非cURL方法,我(不仅如此)在这里找到了该方法:如何使用PHP发送POST请求?

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)

将url用于file_get_contents的选项设置为"on".

Option to use urls for file_get_contents is set to "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);

更改了对克里斯多夫的回答的代码,仍然无法正常工作.据我所知,它也不应该要求访问令牌.我理解这是将通知从中立推送到链接的电子邮件.也许我错了,但是访问令牌不能解决它.

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.

编辑(已解决):访问令牌 IS 需要推送通知,并且由于它不适用于此方法,因此可以与cURL一起使用.

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

推荐答案

您需要为此使用cURL,以便您可以将API密钥作为文档中定义的标头传递:

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

此代码完全是我的头顶,尚未经过测试

我已经分解了选项,以便您可以轻松看到它们,但是您可以将它们组合成一个数组,并通过 curl_setoptarray($ curl,[]);

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, []);

这篇关于发送PHP中的POST请求到pushbullet API 401错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆