【问题标题】:send POST request in PHP to pushbullet API 401 error在 PHP 中发送 POST 请求到 pushbullet API 401 错误
【发布时间】: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


    【解决方案1】:

    您需要为此使用 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, []);传递它们

    【讨论】:

      【解决方案2】:

      听起来您缺少用户帐户的访问令牌。您可以在 https://www.pushbullet.com/account 上找到它。您应该将其包含在“Authorization”之类的标头中:“Bearer ACCESS_TOKEN_HERE”。

      【讨论】:

      • 不幸的是没有做到这一点,但我也认为不需要它,我相应地更新了代码和一个小评论。
      • 您必须是拥有访问令牌的 Pushbullet 用户才能调用 /v2/pushes。您的标题应该更像:'header' => ["Content-type: application/x-www-form-urlencoded", "Authorization: Bearer "],
      • 是的,我绝对需要一个,那里真的错了。它仍然不起作用,但使用 cURL 可以。
      【解决方案3】:

      我知道这是一篇旧帖子,但我在使用与您的网站相同的 Authorization: Bearer APIKEY 方法的 sendgrid 时遇到了同样的问题。

      我终于使用以下代码使用原始 php post 让它工作

      $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,请确保设置 'Content-Type: application/json' 并确保您的 $post_data 在 json 中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-29
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 2021-01-17
        • 2022-08-13
        相关资源
        最近更新 更多