我正在尝试使用 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/