j7mbo / torrent-php
使用 PHP 与各种 torrent 客户端通信的库
Requires
- php: >=5.3.2
- rdlowrey/artax: dev-master
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2020-01-10 15:03:13 UTC
README
提供简单易用的面向对象接口,用于与 torrent 客户端交互。使用此库,您可以以 Torrent
对象检索 Torrent 数据,并告诉您的 torrent 客户端执行 pauseTorrent
和 startTorrent
等操作。
目前支持的客户端 (已启用远程功能)
- Transmission
- Deluge
安装
安装通过 Composer 完成。将以下内容添加到您的 composer.json
文件中
"require": {
"j7mbo/torrent-php": "dev-master"
}
别忘了运行 composer install
以创建包含所有项目依赖项的自动生成的 vendor/
目录。
概述
此库并不特别复杂。它按照以下顺序为您提供 Torrent
对象的数组
ClientTransport
- 从您的客户端检索数据。对于 Transmission 和 Deluge,这通过 HTTP 协议上的 JSON-RPC 调用完成。
ClientAdapter
- 将从传输检索的数据转换为
Torrent
对象。此对象包装传输并使用适配器模式更改输出。
Torrent
您可以得到用于您自己应用的 Torrent
对象。
使用方法
显然,运行 composer install
后的第一件事是包含自动生成的自动加载器文件
require_once "/path/to/project" . "/vendor/autoload.php";
创建一个 ConnectionConfig
对象,带有所需的连接参数。这些参数因客户端而异,所以请检查相关构造函数签名上面的 docblock。以下示例使用 Transmission。
use TorrentPHP\Client\Transmission\ConnectionConfig;
$config = new ConnectionConfig(array(
'host' => 'localhost',
'port' => 9091,
'username' => 'james',
'password' => 'password'
));
我们不是使用 cURL 来进行 RPC 请求,而是使用 Artax。我们需要一个新的 Client
对象和一个新的 Request
对象。
$client = new Artax\Client;
$config = new Artax\Request;
现在,要获取 JSON,请使用 ClientTransport
对象
use TorrentPHP\Client\Transmission\ClientTransport;
$transport = new ClientTransport($client, $request, $config);
在这里,您可以运行 ClientTransport
接口中定义的任何方法,例如
$transport->getTorrents();
$transport->addTorrent('http://urlToTorrentFile.torrent');
以下方法允许您传递 Torrent
对象作为第一个参数,或者将 torrent id(哈希)作为第二个参数
$transport->startTorrent();
$transport->pauseTorrent();
$transport->deleteTorrent();
上述方法都返回客户端提供的原始 json。如果您想得到用于您自己应用中的一致性 Torrent
对象,请在调用之前将传输包装在相关的 ClientAdapter
中。
use TorrentPHP\Clent\Trasmission\ClientAdapter,
TorrentPHP\TorrentFactory,
TorrentPHP\FileFactory;
$adapter = new ClientAdapter($transport, new TorrentFactory, new FileFactory);
然后,只需在适配器上调用相同的方法即可获得漂亮的 Torrent
和 File
对象。简单!
Torrent 对象
Torrent
对象是您最终将获得的实体,包含您选择的通过您选择的 torrent 客户端提供的属性。
所有上述属性都是私有的,可以通过 getter 访问;例如,getHashString()
和 getName()
。
查看 Torrent
类以查看获取所需数据的可用方法。
代码示例
require_once __DIR__ . "/vendor/autoload.php";
use TorrentPHP\Client\Transmission\ConnectionConfig,
TorrentPHP\Client\Transmission\ClientTransport,
TorrentPHP\Client\Transmission\ClientAdapter,
TorrentPHP\TorrentFactory,
TorrentPHP\FileFactory;
// Create the HTTP Client Object
$client = new Artax\Client;
// Create the HTTP Client Request
$request = new Artax\Request;
// Configuration
$config = new ConnectionConfig(array(
'host' => 'localhost',
'port' => 9091,
'username' => 'james',
'password' => 'password'
));
// Create the transport that returns json
$transport = new ClientTransport($client, $request, $config);
// Create the adapter that returns Torrent objects
$adapter = new ClientAdapter($transport, new TorrentFactory, new FileFactory);
// Add a torrent, and get back a Torrent object
$torrent = $adapter->addTorrent('http://releases.ubuntu.com/14.04/ubuntu-14.04-server-i386.iso.torrent');
// Pause the torrent we just added
$torrent = $adapter->pauseTorrent($torrent);
// Start the torrent we just added
$torrent = $adapter->startTorrent($torrent);
// Delete a torrent by it's hash instead of the object
$adapter->deleteTorrent(null, $torrent->getHashString());
Torrent 客户端
Transmission 需要 transmission-remote-gui
安装,并设置配置以允许远程连接。
Deluge 需要 deluged
和 deluge-web
运行。
异步调用
喜欢事件驱动编程和回调地狱?你可以使用 TorrentPHP 来进行异步调用。这些调用是通过 Artax 和 Alert 来实现的。实际上,你传递一个 PHP 可调用对象(需要 PHP 5.4),只有在收到响应时才会调用这个对象。它是不阻塞的,并且非常酷。
目前只有 getTorrents()
支持异步调用。以下是一个向 Deluge 发起异步调用的示例
use TorrentPHP\Client\Deluge\ConnectionConfig,
TorrentPHP\Client\Deluge\AsyncClientTransport,
TorrentPHP\Client\Deluge\AsyncClientFactory,
Alert\ReactorFactory;
// Factory to create the async client
$clientFactory = new TorrentPHP\Client\AsyncClientFactory;
// We need a request object as before
$request = new Artax\Request;
// Our connection settings
$config = new ConnectionConfig([
'host' => 'localhost',
'port' => 8112,
'password' => 'deluge'
]);
// The new AsyncClientTransport
$transport = new AsyncClientTransport($clientFactory, $request, $config);
// Our own function gets invoked on the async response. The response is "injected" into the first variable for us.
$callable = function($response) {
echo 'This could have taken 5 seconds or more, but it's async, so it's non-blocking. Yay!' . PHP_EOL;
var_dump($response);
};
// Let's get our torrents asynchronously - the new AsyncClientTransport object takes a second callable parameter
$transport->getTorrents([], $callable);
扩展
你可以通过在 src/Client/<ClientName>
中创建一个新的客户端目录,并添加 ClientAdapter
、ClientTransport
和 ConnectionConfig
来添加对您自己客户端的支持。然而,它们不需要通过 RPC 来通信,因为实现的处理方式与应用程序的其他部分是分开的。
请确保使用正确的命名空间,并实现/扩展正确的类。查看 src/Transmission
和 src/Deluge
目录中的示例。
如果您想为不同的 torrent 客户端添加支持,但不知道如何自行进行,请随时提交一个问题,我们可以一起解决这个问题。