【问题标题】:Why does Telegram give me an empty POST request on the webhook?为什么 Telegram 在 webhook 上给我一个空的 POST 请求?
【发布时间】:2021-06-17 11:36:17
【问题描述】:

我按照以下步骤操作:

  1. 向 BotFather 注册了一个机器人。
  2. 发送 Webhook (https://api.telegram.org/bot[BOTID]/setWebhook) 的 post 请求,URL 为 https://example.com/myscript.php
  3. 用 getWebhookInfo 仔细检查,发现它已正确注册。
  4. 当我向机器人发送消息时,正在调用脚本,但 POST 有效负载为空。在documentation 中,他们说他们会向指定的 url 发送一个 HTTPS POST 请求,其中包含一个 JSON 序列化更新。

还有其他人有这个问题,也许知道解决这个问题的方法吗?

我要记录的 php 脚本:

    $file = dirname(__FILE__) . '/telegram-log.txt';
    $entry = (object)array();
    $entry->date = date("r");
    $entry->GET = $_GET;
    $entry->POST = $_POST;
    $entry->REQUEST = $_REQUEST;
    $entry->HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
    $entry->REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
    file_put_contents($file, json_encode($entry) . "\n", FILE_APPEND | LOCK_EX);

回复:

{"date":"Thu, 17 Jun 2021 13:42:49 +0200","GET":[],"POST":[],"REQUEST":[],"HTTP_USER_AGENT":null,"REMOTE_ADDR":"91.108.6.133"}

【问题讨论】:

    标签: telegram-bot telegram-webhook


    【解决方案1】:

    使用$content = file_get_contents("php://input") 接收更新。

    Telegram 的响应是内容类型application/json

    $_POST 仅在使用 application/x-www-form-urlencodedmultipart/form-data 作为请求中的 HTTP Content-Type 时有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-09
      • 1970-01-01
      • 2020-11-13
      相关资源
      最近更新 更多