vlsv/telegram-data-validator

Telegram 数据验证器是一个用于验证从 Telegram 小程序接收到的 initData 完整性的 PHP 库。它使用 HMAC-SHA-256 进行数据完整性检查。

v1.0.4 2024-01-13 21:24 UTC

This package is auto-updated.

Last update: 2024-09-13 22:53:51 UTC


README

workflow

Telegram 数据验证器

Telegram 数据验证器是一个用于验证从 Telegram 小程序接收到的 initData 完整性的 PHP 库。它使用 HMAC-SHA-256 进行数据完整性检查。

安装

使用 Composer 安装库

composer require vlsv/telegram-data-validator

文档

/**
 * Validates the integrity of the provided Telegram WebApp initData string received from a Telegram
 * mini-application.
 *
 * @param string $initData The initData string containing query parameters.
 * @param string $botToken The bot token used for HMAC calculation.
 * @param bool   $verbose  Whether to include additional information in the result.
 *
 * @return bool|array If $verbose is true, returns an associative array with validation information,
 *                    otherwise returns a boolean indicating whether the validation passed.
 */
InitData::isValid(string $initData, string $botToken, bool $verbose = false): bool|array

用法

<?php

use Vlsv\TelegramInitDataValidator\Validator\InitData;

// Your bot token
$botToken = "<your-bot-token>";

// Your initData string
$initData = "query_id=AAGk...";

// Validate initData
$result = InitData::isValid($initData, $botToken, true);

// Display the validation result
var_dump($result);

测试

composer tests

许可协议

本项目遵循 GNU 通用公共许可证 v3 许可。