skip to Main Content

I am trying to create a Telegram Bot. I follow this video. my codes are working in localhost, but when I put them on server the result is different. this code just call getUpdates method of Telegram api.

Code :

<?php 

    $botToken = "146158152:AAHO**********-L3xF08RN7H0xK8E";
    $website = "https://api.telegram.org/bot".$botToken;

    $update = file_get_contents($website."/getUpdates");

    var_dump($update);

?>

Localhost result :

string(616) "{"ok":true,"result":[{"update_id":35****293, "message":{"message_id":1,"from":{"id":95*****4,"first_name":"Mahmood","last_name":"Kohansal","username":"mahmoodkohansal"},"chat":{"id":95*****4,"first_name":"Mahmood","last_name":"Kohansal","username":"mahmoodkohansal","type":"private"},"date":1448737853,"text":"/start"}},{"update_id":356676294, "message":{"message_id":2,"from":{"id":95*****4,"first_name":"Mahmood","last_name":"Kohansal","username":"mahmoodkohansal"},"chat":{"id":95881214,"first_name":"Mahmood","last_name":"Kohansal","username":"mahmoodkohansal","type":"private"},"date":1448737855,"text":"1"}}]}"

and Server result :

bool(false)

Sorry for my poor English.

2

Answers


  1. Chosen as BEST ANSWER

    PHP file_get_contents method was the problem. I found the same problem with this method here, and use the solution to solve my problem.


  2. If your code works in localhost, the first assumption would be that your server was not successful in establishing a connection to the bot api.
    Perhaps you should put it in an if statement.

    $token = "your token";
    $website = "https://api.telegram.org/bot".$token;
    
    if($updates = file_get_contents($website."/getUpdates"))
    {
      echo "Connection made";
    }
    else
    {
      echo "Fail";
    }
    

    Also can you make sure a webHook isn’t set? getUpdates method does not return results if a webHook is set.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search