christeredvartsen / php-bittorrent
一套可以用来与种子文件(读写)交互的组件,以及可以将数据编码/解码为/从 BitTorrent 格式的类。
v2.0.0
2020-01-21 19:12 UTC
Requires
- php: >=7.2
Requires (Dev)
- phploc/phploc: ^5.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-08-29 03:20:55 UTC
README
PHP BitTorrent 是一套可以用来与种子文件(读写)交互以及将数据编码/解码为/从 BitTorrent 格式 的组件。
需求
PHP BitTorrent 需要 PHP 7.2 或更高版本。
安装
可以使用 Composer 安装 PHP BitTorrent。
composer require christeredvartsen/php-bittorrent ^2.0
使用 PHP BitTorrent API
编码 PHP 变量
<?php require 'vendor/autoload.php'; $encoder = new BitTorrent\Encoder(); var_dump($encoder->encodeString('Some string')); // string(14) "11:Some string" var_dump($encoder->encodeInteger(42)); // string(4) "i42e" var_dump($encoder->encodeList([1, 2, 3]); // string(11) "li1ei2ei3ee" var_dump($encoder->encodeDictionary(['foo' => 'bar', 'bar' => 'foo']); // string(22) "d3:foo3:bar3:bar3:fooe"
在 BitTorrent\Encoder
类中,还有一个名为 encode
的便捷方法,可以用来编码所有可编码的变量(整数、字符串和数组)。
解码 BitTorrent 编码的数据
<?php require 'vendor/autoload.php'; $decoder = new BitTorrent\Decoder(); var_dump($decoder->decodeString('11:Some string')); // string(11) "Some string" var_dump($decoder->decodeInteger('i42e')); // int(42) var_dump($decoder->decodeList('li1ei2ei3ee'); // array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } var_dump($decoder->decodeDictionary('d3:foo3:bar3:bar3:fooe'); // array(2) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "foo" }
还有一个名为 decode
的便捷方法,可以解码任何 BitTorrent 编码的数据。
解码种子文件
解码器类还有一个方法可以解码种子文件(这是一个编码字典)。
<?php require 'vendor/autoload.php'; $decoder = new BitTorrent\Decoder(); $decodedFile = $decoder->decodeFile('/path/to/file.torrent');
创建新的种子文件和打开现有的种子文件
BitTorrent\Torrent
类表示一个种子文件,可以用来创建种子文件。
<?php require 'vendor/autoload.php'; $torrent = BitTorrent\Torrent::createFromPath('/path/to/files', 'http://tracker/announce.php') ->withComment('Some comment'); $torrent->save('/save/to/path/file.torrent');
该类还可以加载种子文件
<?php require 'vendor/autoload.php'; $torrent = BitTorrent\Torrent::createFromTorrentFile('/path/to/file.torrent') ->withAnnounce('http://tracker/announce.php') // Override announce in original file ->withComment('Some comment'); // Override commend in original file $torrent->save('/save/to/path/file.torrent'); // Save to a new file
许可证
在 MIT 许可证下发布。
请参阅 LICENSE 文件。