skip to Main Content

I’m trying to use the Telegram API to make an online advertising app with PHP, but the problem I have is that I can’t even understand making request to telegram website. This is a short code I wrote based on Telegram’s API and protocol:

<!DOCTYPE html>
    <meta http-equiv="Content-Length" content="348">
    <meta http-equiv="Connection" content="keep-alive">
    <meta http-equiv="Host" content="">

$url = '';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);

$result = curl_exec($curl);

echo $result;


Does anyone have any idea how to make it work?



  1. The Telegram API is a pain to use, you have to apply all sort of encryption sorcery to work with their MTProto protocol and there’s very little reference or example for PHP available. I would suggest you use their new Bot API. It is a service the created that abstracts all the MTProto interactions behind a simple HTTP layer. You first need to generate a bot using their Bot Father and then you use the ID to interact with the API.

    Receiving new messages (polling):

    $bot_id = "<bot ID generated by BotFather>";
    # Note: you want to change the offset based on the last update_id you received
    $url = '' . $bot_id . '/getUpdates?offset=0';
    $result = file_get_contents($url);
    $result = json_decode($result, true);
    foreach ($result['result'] as $message) {

    Sending messages:

    # The chat_id variable will be provided in the getUpdates result
    $url = '' . $bot_id . '/sendMessage?text=message&chat_id=0';
    $result = file_get_contents($url);
    $result = json_decode($result, true);

    You can also use a webhook instead of polling for updates. You can find more information in the API documentation I linked.

    Login or Signup to reply.
  2. You can use this library:

    PHP implementation of the telegram MTProto protocol (better tg-cli)

    Simple sample code:

    if (!file_exists('madeline.php')) {
        copy('', 'madeline.php');
    include 'madeline.php';
    $MadelineProto = new danogMadelineProtoAPI('session.madeline');
    $me = $MadelineProto->get_self();
    if (!$me['bot']) {
        $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => "Hi!nThanks for creating MadelineProto! <3"]);
        $MadelineProto->channels->joinChannel(['channel' => '@MadelineProto']);
        try {
            $MadelineProto->messages->importChatInvite(['hash' => '']);
        } catch (danogMadelineProtoRPCErrorException $e) {
        $MadelineProto->messages->sendMessage(['peer' => '', 'message' => 'Testing MadelineProto!']);
    echo 'OK, done!'.PHP_EOL;
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top