使用PHP调用Telegram API

4

我正在尝试使用Telegram API和PHP制作一个在线广告应用程序,但我遇到的问题是我甚至不知道如何向Telegram网站发出请求。以下是我根据Telegram的API和协议编写的简短代码:

<!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>

有人有任何想法如何让它发挥作用吗?

1
-.-- --- ..- / ... .... --- ..- .-.. -.. / .- ... -.- / - .... . / ...- . -. -.. --- .-. .-.-.-你应该询问供应商,他们是否有支持论坛?否则,您是否遇到任何错误?请更具体地描述。您是否只需要告诉cURL返回传输?可能需要使用cURL设置选项 - ficuscr
不返回论坛,只是在“nginx/0.3.33”上返回“501 Not Implemented”,根据我了解的协议,我们应该建立一个持久连接并向他们提供的URL发起POST请求。 - Amirmasoud
啊,也许你需要告诉 cURL 跟随重定向?CURLOPT_FOLLOWLOCATION 另外,如果路由是 HTTPS,可能需要告诉 cURL 忽略证书验证。在发出请求时不需要其他身份验证吗? - ficuscr
我将curl_setopt()更改为curl_setopt_array($ curl,array( CURLOPT_URL => $ url, CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_SSL_VERIFYHOST => FALSE )); 仍然无效。 - Amirmasoud
https://dev59.com/z2865IYBdhLWcg3wkPSD - ficuscr
显示剩余3条评论
2个回答

6
Telegram API很难使用,您需要应用各种加密技术才能使用他们的MTProto协议,并且很少有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']);

您也可以使用 Webhook 而不是轮询更新。您可以在我提供的 API 文档中找到更多信息。

这个API没有我想要的方法,即通过频道/ID获取消息信息的方法,所以我需要回答作者最初的问题。 :/ - dw1

0
你可以使用这个库:
PHP实现的电报MTProto协议(更好的tg-cli) https://github.com/danog/MadelineProto 简单的样例代码:
<?php

if (!file_exists('madeline.php')) {
    copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
include 'madeline.php';

$MadelineProto = new \danog\MadelineProto\API('session.madeline');
$MadelineProto->start();

$me = $MadelineProto->get_self();

\danog\MadelineProto\Logger::log($me);

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' => 'https://t.me/joinchat/Bgrajz6K-aJKu0IpGsLpBg']);
    } catch (\danog\MadelineProto\RPCErrorException $e) {
    }

    $MadelineProto->messages->sendMessage(['peer' => 'https://t.me/joinchat/Bgrajz6K-aJKu0IpGsLpBg', 'message' => 'Testing MadelineProto!']);
}
echo 'OK, done!'.PHP_EOL;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接