4

我正在尝试在 Telegram 中迈出第一步,我也是 PHP 的新手......

我已经在我的 Windows 7 电脑上配置了带有 PHP 5.6.14 和 SSL 的 Apache 2.4,它在 http 和 https 中运行良好。

然后我尝试遵循这个 Telegram Bot 教程https://www.youtube.com/watch?v=hJBYojK7DO4。一切正常,直到我必须创建一个像这样的简单 PHP 程序

<?php
  $botToken = "<my_bot_token>";
  $website = "https://api.telegram.org/bot".$botToken; 
  $update = file_get_contents($website."/getUpates"); 
  print_r($update);
?>

当我尝试放入浏览器时

https://localhost/Telegram/MyYouTubeTutorialBot/YouTubeTutorialBot.php

回应是

Warning: file_get_contents(https://api.telegram.org/<my_bot_token>/getupates): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in <my_php_file_location> on line 6

我在网上搜索过类似的问题,但没有解决任何问题:最有趣的回答是这个问题file_get_contents - failed to open stream: HTTP request failed!HTTP/1.1 404 Not Found但我不明白如何使其适应我的情况。

在其他回复中有使用 curl 的建议,但我想解决持续的 file_get_contents 功能。

我认为这不是 PHP 问题,而是我的配置中某处的问题......但我不知道在哪里

有什么建议么?

非常感谢您提前

切萨雷

增加注释

正如@aeryaguzov 在评论中建议的那样,我的原始代码中有拼写错误....

这是您现在可以使用的固定代码...

<?php
  $botToken = "<my_bot_token>";
  $website = "https://api.telegram.org/bot".$botToken; 
  $update = file_get_contents($website."/getUpdates"); 
  print_r($update);
?>
4

3 回答 3

2

这不是您的配置中的 PHP 问题。该错误意味着该文件https://api.telegram.org/<my_bot_token>/getupates不存在。

于 2015-10-26T08:38:44.277 回答
0

Telegram API 总是在它的正文中返回一些东西。如果出现错误,这是一个 JSON 对象,其中ok设置为false、anerror_code和一个description字段。但是,它还将响应头设置为相关的错误代码,导致file_get_content()抛出错误而不是返回仍然非常有用的主体。为了避免这种情况,您可以像这样添加流上下文:

<?php
    $stream_context = stream_context_create(array(
        'http' => array (
            'ignore_errors' => true
         )
    ));

    $botToken = "<my_bot_token>";
    $website = "https://api.telegram.org/bot".$botToken;
    $update = file_get_contents($website."/getUpdates", false, $stream_context);
    print_r($update);
?>
于 2016-10-16T23:59:31.083 回答
-1

我得到了同样的错误。您可以尝试使用 JSON 进行检查,这是因为 webhook 正在运行

{"ok":false,"error_code":409,"description":"Conflict: can't use getUpdates method while webhook is active"}

您应该通过
...../setwebhook 删除您的机器人 API 的 webhook,而无需 url。

于 2017-03-22T01:58:18.397 回答