php - 在 PHP 中使用 Telegram API

标签 php curl telegram

我正在尝试使用 Telegram API 通过 PHP 制作在线广告应用程序,但我遇到的问题是我什至无法理解向 Telegram 网站发出请求。这是我根据 Telegram 的 API 和协议(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>

有人知道如何让它工作吗?

最佳答案

Telegram API 使用起来很麻烦,您必须应用各种加密魔法才能使用他们的 MTProto 协议(protocol),并且很少有可用的 PHP 引用或示例。我建议您使用他们的新 Bot API .它是一种创建的服务,将所有 MTProto 交互抽象为一个简单的 HTTP 层。您首先需要使用他们的 Bot Father 生成一个机器人然后您使用该 ID 与 API 进行交互。

接收新消息(轮询):

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

发送消息:

# 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']);

您还可以使用网络钩子(Hook)代替轮询更新。您可以在我链接的 API 文档中找到更多信息。

关于php - 在 PHP 中使用 Telegram API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31190994/

相关文章:

php - 在 JavaScript 中使用从 MySQL 检索的数据?

php - 为什么我的 PHP 脚本间歇性地无法通过 FTP 上传文件?

linux - 每 5 秒运行一次 cURL 命令

php - QuickBlox 用户注册问题 REST API

python - 我正在用 python 写一个 Telegram Bot

android - Telegram API 入门 : Any useful resource?

php - 如何使用 PHP 删除上下文中不可读的字符?

php - Mysql - 验证结果的一部分是否存在于另一个表中

linux - 如何在不使用服务器端缓存的情况下调用 cURL?

c# - Telegram bot - 向/启动命令添加唯一键