【发布时间】:2015-05-26 23:39:44
【问题描述】:
我正在尝试使用 pushbullet 发送简单的推送通知,只需使用电子邮件并将其发送到链接的帐户,以避免需要帐户数据。 (请参阅此处的参考:https://docs.pushbullet.com/#pushes)
因此,我在 php 中使用了我(不仅)在这里找到的非 cURL 方法: 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 的响应,仍然无法正常工作。据我了解,它也不应该需要访问令牌。我将其理解为将通知从中立推送到链接的电子邮件。也许我错了,但访问令牌并不能解决它。
EDIT(已解决):推送通知需要一个访问令牌IS,因为它不适用于此方法,但它适用于 cURL。
【问题讨论】:
标签: php api post pushbullet