I'm trying to move my first steps in Telegram and I'm also a newbie in PHP ......
我正试图在Telegram中移动我的第一步,我也是PHP的新手......
I've configured, on my Windows 7 pc, Apache 2.4 with PHP 5.6.14 and SSL and it's working fine in http and https.
我已经在我的Windows 7 PC上配置了Apache 2.4 with PHP 5.6.14和SSL,它在http和https中运行良好。
Then I've tried to follow this Telegram Bot Tutorial https://www.youtube.com/watch?v=hJBYojK7DO4. Everything works fine until when I have to create a simple PHP program like this one
然后我试着按照这个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);
?>
When I try to put in my browser
当我尝试放入浏览器时
https://localhost/Telegram/MyYouTubeTutorialBot/YouTubeTutorialBot.php
the response is
反应是
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
I've searched on the web for similar issues but nothing has solved: the most interesting response is in this question file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found but I don't understand how to adapt it to my case.
我在网上搜索了类似的问题,但没有解决:最有趣的回答是在这个问题中file_get_contents - 无法打开流:HTTP请求失败! HTTP / 1.1 404 Not Found但我不明白如何使其适应我的情况。
In others responses there is the suggestion to use curl but I'd like to solve continuing file_get_contents function.
在其他响应中有建议使用curl,但我想解决继续file_get_contents函数。
I think that it's not a PHP problem but something in my configurations somewhere ... bu I don't know where
我认为这不是一个PHP问题,而是我的配置中的某些东西......我不知道在哪里
Any suggestions?
Thank you very much in advance
非常感谢你提前
Cesare
NOTE ADDED
There was a typo spelling error in my original code as @aeryaguzov suggest in the comments ....
我的原始代码中出现了拼写错误,因为@aeryaguzov在评论中提出了....
Here you're the fixed code that works right now ...
在这里,你是现在可以使用的固定代码......
<?php
$botToken = "<my_bot_token>";
$website = "https://api.telegram.org/bot".$botToken;
$update = file_get_contents($website."/getUpdates");
print_r($update);
?>
3 个解决方案
#1
2
It's not a PHP problem something in your configurations. the error means that the file https://api.telegram.org/<my_bot_token>/getupates
does not exist.
在您的配置中,这不是PHP问题。错误意味着文件https://api.telegram.org/
#2
0
The Telegram API always returns something in it's body. In the case of an error this is a JSON object with ok
set to false
, an error_code
and a description
field. However, it also sets the response header to the relevant error code, causing file_get_content()
to throw an error instead of returning the still very useful body. To circument this you can add a stream context like so:
Telegram API总是在它的主体中返回一些东西。在出现错误的情况下,这是一个将ok设置为false的JSON对象,一个error_code和一个描述字段。但是,它还将响应头设置为相关的错误代码,导致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);
?>
#3
-1
I got the same error. You can Try check with JSON then it is because of the webhook is running
我得到了同样的错误。您可以尝试使用JSON检查然后是因为webhook正在运行
{"ok":false,"error_code":409,"description":"Conflict: can't use getUpdates method while webhook is active"}
you should delete the webhook for your bot API by
...../setwebhook without url.
你应该删除你的机器人API的webhook ..... / setwebhook没有url。
#1
2
It's not a PHP problem something in your configurations. the error means that the file https://api.telegram.org/<my_bot_token>/getupates
does not exist.
在您的配置中,这不是PHP问题。错误意味着文件https://api.telegram.org/
#2
0
The Telegram API always returns something in it's body. In the case of an error this is a JSON object with ok
set to false
, an error_code
and a description
field. However, it also sets the response header to the relevant error code, causing file_get_content()
to throw an error instead of returning the still very useful body. To circument this you can add a stream context like so:
Telegram API总是在它的主体中返回一些东西。在出现错误的情况下,这是一个将ok设置为false的JSON对象,一个error_code和一个描述字段。但是,它还将响应头设置为相关的错误代码,导致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);
?>
#3
-1
I got the same error. You can Try check with JSON then it is because of the webhook is running
我得到了同样的错误。您可以尝试使用JSON检查然后是因为webhook正在运行
{"ok":false,"error_code":409,"description":"Conflict: can't use getUpdates method while webhook is active"}
you should delete the webhook for your bot API by
...../setwebhook without url.
你应该删除你的机器人API的webhook ..... / setwebhook没有url。