Cesare
Cesare

Reputation: 1749

Telegram + PHP (Windows 7): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

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

Upvotes: 5

Views: 3264

Answers (3)

DrFendi Ameen
DrFendi Ameen

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

Upvotes: -1

Senshi
Senshi

Reputation: 37

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

Upvotes: 0

Rao Lanffy
Rao Lanffy

Reputation: 273

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.

Upvotes: 2

Related Questions