【问题标题】:How to read data from a form?如何从表单中读取数据?
【发布时间】:2016-08-18 00:02:59
【问题描述】:

我有一个 php 脚本,可以让我的电报机器人发送消息:

<?php
$botToken = "<my_bot_token_here>";
$website = "http://api.telegram.org/bot".$botToken;
$chat_id = 25665198;
file_get_contents($website.'/sendmessage?chat_id='.$chat_id.'&text='.$_POST["message"]);

我想决定发送什么消息,所以我创建了一个表单:

 <form action="telegram.php" method="post">
     Message: <input type="text" name="message" />
     <input type="submit">
  </form>

但由于某种原因,该脚本不起作用。它出什么问题了?

【问题讨论】:

  • var_dump post 变量以查看是否有东西被发送。其他一些提示:不要公开您的私人机器人令牌。转义 URL 参数。
  • 您应该对telegram.php 进行一些检查,以验证用户身份。目前这将允许任何用户/机器人发送消息。
  • 您必须为电报 API 使用 https 方案:https://api.telegram.org/bot&lt;token&gt;/METHOD_NAME - 在文档中

标签: php telegram-bot


【解决方案1】:

也许只是 https 而不是 $website 上的 http。所以改成:

$website = "https://api.telegram.org/bot".$botToken;

另外把最后一行改成这样:

$output = file_get_contents($website.'/sendmessage?chat_id='.$chat_id.'&text='.$_POST["message"]);
if($output == false) ? echo "Something gone Wrong" : echo $output;

这样你会看到file_get_contents()

的结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多