4

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);
?>
Community
  • 1
  • 1
Cesare
  • 1,629
  • 9
  • 30
  • 72
  • I see in your warning. Do you send real token instead of this placeholder? – aeryaguzov Oct 26 '15 at 08:27
  • yes! there is a real token ....... – Cesare Oct 26 '15 at 08:28
  • maybe you're just using the wrong url...try $website = "https://api.telegram.org/bot/".$botToken.'/'; , also, what does var_dump(base64_encode($website)); give you? – hanshenrik Oct 26 '15 at 08:46
  • I've tried but nothing change .... the result of var_dump(base64_encode($website)); is the following ......string(100) "aHR0cHM6Ly9hcGkudGVsZWdyYW0ub3JnL2JvdDE3MDE4NDEwMzpBQUVKbUVzbXo1MVVFbTM5aDc2T3NxcUlrVEZGX1MzNUQ4dw==" – Cesare Oct 26 '15 at 09:00
  • I checked telegram API and see only "getUpdates" method but not "getupates". Check spelling please – aeryaguzov Oct 26 '15 at 09:02
  • @aeryaguzov thanks .... good notes, but it's not the solution unfortunately .... in any case I've fix my code (also in my initial question ....), and now I'm using getUpdate function .... – Cesare Oct 26 '15 at 09:11
  • 1
    @Cesare you have invalid spelling again: use "getUpdates" but not "getUpates" – aeryaguzov Oct 26 '15 at 09:23
  • @aeryaguzov .... You're right!!!! A very, great, STUPID error ..... sorry!! But thanks a lot for the support.... .Now it's working fine ... – Cesare Oct 26 '15 at 09:31

3 Answers3

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.

Rao Lanffy
  • 273
  • 1
  • 2
  • 8
  • 1
    I don't think so because putting the same http request in a browser give a right response .... – Cesare Oct 26 '15 at 08:44
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);
?>
Senshi
  • 27
  • 6
-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.

Aan Faisal
  • 31
  • 1
  • 7