douzhi1937 2015-07-02 17:29
浏览 109
已采纳

使用PHP的电报API

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>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Length" content="348">
    <meta http-equiv="Connection" content="keep-alive">
    <meta http-equiv="Host" content="149.154.167.40:80">
</head>

<body>
<?php
$url = '149.154.167.40';

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

$result = curl_exec($curl);

echo $result;

?>
</body>
</html>

Does anyone have any idea how to make it work?

  • 写回答

2条回答 默认 最新

  • doushou6480 2015-07-02 21:06
    关注

    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):

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

    Sending messages:

    # The chat_id variable will be provided in the getUpdates result
    $url = 'https://api.telegram.org/bot' . $bot_id . '/sendMessage?text=message&chat_id=0';
    $result = file_get_contents($url);
    $result = json_decode($result, true);
    
    var_dump($result['result']);
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元