stormwalkerec / php-bittorrent
PHP\BitTorrent 是一组组件,可以用来与 torrent 文件(读写)交互,以及将数据编码和解码成 BitTorrent 格式。
Requires
- php: >=5.3.2
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-24 07:52:36 UTC
README
PHP_BitTorrent 是一组组件,可以用来与 torrent 文件(读写)交互,以及将数据编码和解码成 BitTorrent 格式。
要求
PHP_BitTorrent 需要 PHP 5.3.x 或更高版本。推荐版本是 5.3.2 或更新版本。
安装
PHP_BitTorrent 可以使用 PEAR、Composer 或 PHAR 安装。
PEAR
sudo pear config-set auto_discover 1
sudo pear install --alldeps pear.starzinger.net/PHP_BitTorrent
Composer
在您的依赖项中指定 christeredvartsen/php-bittorrent
。不同的版本可以在 Packagist 网站上找到。
PHAR
您还可以下载 php-bittorrent.phar 并在需要使用 PHP_BitTorrent 的位置引入该文件。
<?php require '/path/to/php-bittorrent.phar'; $encoder = new PHP\BitTorrent\Encoder(); // ...
使用 PHP BitTorrent API
自动加载器
PHP BitTorrent 并不带自己的自动加载器,因此您需要使用 PSR-0 兼容的自动加载器才能使所有功能按预期工作,或者提供您自己的 require[_once]
语句。一个这样的自动加载器示例可以在 这里 找到。当作为 PHAR 归档使用 PHP_BitTorrent 时,您只需引入归档本身即可,当使用 Composer 安装时,可以简单地引入 Composer 生成的自动加载器(vendor/autoload.php
)。
编码 PHP 变量
<?php $encoder = new PHP\BitTorrent\Encoder(); var_dump($encoder->encodeString('Some string')); // string(14) "11:Some string" var_dump($encoder->encodeInteger(42)); // int(42) var_dump($encoder->encodeList(array(1, 2, 3)); // string(11) "li1ei2ei3ee" var_dump($encoder->encodeDictionary(array('foo' => 'bar', 'bar' => 'foo')); // string(22) "d3:foo3:bar3:bar3:fooe"
在 PHP\BitTorrent\Encoder
类中还有一个简单的 encode
方法,可以用来编码所有可编码的变量(整数、字符串和数组)。
解码 BitTorrent 编码数据
<?php $encoder = new PHP\BitTorrent\Encoder(); $decoder = new PHP\BitTorrent\Decoder($encoder); // The decoder needs an encoder for some methods 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 编码数据。
解码 torrent 文件
解码器类还有一个方法可以解码 torrent 文件(这是一个编码的字典)。
<?php $encoder = new PHP\BitTorrent\Encoder(); $decoder = new PHP\BitTorrent\Decoder($encoder); $decodedFile = $decoder->decodeFile('/path/to/file.torrent');
创建新的 torrent 文件和打开现有的文件
PHP\BitTorrent\Torrent
类代表一个 torrent 文件,可以用来创建 torrent 文件。
<?php $torrent = PHP\BitTorrent\Torrent::createFromPath('/path/to/files', 'http://tracker/announce.php'); $torrent->setComment('Some comment') ->save('/save/to/path/file.torrent');
该类还可以加载 torrent 文件
<?php $torrent = PHP\BitTorrent\Torrent::createFromTorrentFile('/path/to/file.torrent'); $torrent->setAnnounce('http://tracker/announce.php') // Override announce in original file ->setComment('Some comment') // Override commend in original file ->save('/save/to/path/file.torrent'); // Save to a new file
32 位平台
在 32 位平台上,这些组件在整数处理方面略有不同
- 通用的
PHP\BitTorrent\Encoder::encode
方法会将整数和包含数字的字符串编码为字符串(包含浮点值的字符串仍被视为常规字符串)。 PHP\BitTorrent\Decoder::decodeInteger
方法将返回字符串值,而不是整数。PHP\BitTorrent\Torrent::getSize
方法将使用 bcadd 函数来计算 torrent 中文件的大小。