5

I'm trying to move my first steps in Telegram and I'm also a newbie in 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.

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

<?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.

In others responses there is the suggestion to use curl but I'd like to solve continuing file_get_contents function.

I think that it's not a PHP problem but something in my configurations somewhere ... bu I don't know where

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 ....

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);
?>
8
  • I see <my_bot_token> in your warning. Do you send real token instead of this placeholder? Commented Oct 26, 2015 at 8:27
  • yes! there is a real token ....... Commented Oct 26, 2015 at 8:28
  • maybe you're just using the wrong url...try $website = "api.telegram.org/bot/".$botToken.''; , also, what does var_dump(base64_encode($website)); give you? Commented Oct 26, 2015 at 8:46
  • I've tried but nothing change .... the result of var_dump(base64_encode($website)); is the following ......string(100) "aHR0cHM6Ly9hcGkudGVsZWdyYW0ub3JnL2JvdDE3MDE4NDEwMzpBQUVKbUVzbXo1MVVFbTM5aDc2T3NxcUlrVEZGX1MzNUQ4dw==" Commented Oct 26, 2015 at 9:00
  • 1
    @Cesare you have invalid spelling again: use "getUpdates" but not "getUpates" Commented Oct 26, 2015 at 9:23

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think so because putting the same http request in a browser give a right response ....
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:

<?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);
?>

Comments

-1

I got the same error. You can Try check with JSON then it is because of the webhook is running

{"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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.