要从Telegram中的@username
获取用户ID,你需要使用Telegram Bot API。以下是使用PHP实现这一功能的基本步骤:
以下是一个简单的PHP脚本示例,用于获取用户ID:
<?php
$botToken = 'YOUR_BOT_TOKEN'; // 替换为你的机器人Token
$username = 'target_username'; // 替换为目标用户名
// 构建API请求URL
$url = "https://api.telegram.org/bot$botToken/getUsers?username=$username";
// 发送GET请求
$response = file_get_contents($url);
$data = json_decode($response, true);
if ($data['ok'] && !empty($data['result'])) {
$userId = $data['result']['id'];
echo "用户ID: $userId";
} else {
echo "未找到用户或发生错误。";
}
?>
通过以上步骤和代码示例,你应该能够从Telegram中的@username
获取到用户ID。记得在实际部署时,要考虑到安全性和用户体验。
领取专属 10元无门槛券
手把手带您无忧上云