使用电报API与PHP [英] Using telegram API with PHP

查看:351
本文介绍了使用电报API与PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用电报API制作一个在线广告应用程序与PHP,但我的问题是,我甚至不能理解请求电报网站。这是一个基于Telegram的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?

推荐答案

电报API是一个痛苦的使用,你必须应用所有类型的加密巫术来使用他们的MTProto协议,并有很少的参考或PHP的可用示例。我建议您使用新的 Bot API 。它是一个服务,创建抽象所有的MTProto互动后面一个简单的HTTP层。您首先需要使用其 Bot父亲生成漫游器,然后使用ID与API。

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.

接收新邮件(轮询)

<?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文档中找到更多信息。

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

这篇关于使用电报API与PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆