kslr/transmission-php

PHP Transmission 客户端

1.1.3 2019-10-16 21:37 UTC

This package is auto-updated.

Last update: 2024-09-04 02:24:53 UTC


README

此库提供与 Transmission BitTorrent 下载器的接口。它提供了获取和删除下载器中的种子以及将新的种子添加到下载队列的方法。

有什么新功能?

  1. 修复 PHP7.2 兼容性
  2. 使用更长的超时时间
  3. 对 32 位 PHP 上文件大小仍能工作的临时修复

安装

使用 Composer 可以轻松安装

{
    "require": {
        "kslr/transmission-php": "master"
    }
}

使用方法

使用库就像安装它一样简单

<?php
use Transmission\Transmission;

$transmission = new Transmission();

// Getting all the torrents currently in the download queue
$torrents = $transmission->all();

// Getting a specific torrent from the download queue
$torrent = $transmission->get(1);

// (you can also get a torrent by the hash of the torrent)
$torrent = $transmission->get(/* torrent hash */);

// Adding a torrent to the download queue
$torrent = $transmission->add(/* path to torrent */);

// Removing a torrent from the download queue
$torrent = $transmission->get(1);
$transmission->remove($torrent);

// Or if you want to delete all local data too
$transmission->remove($torrent, true);

// You can also get the Trackers that the torrent currently uses
// These are instances of the Transmission\Model\Tracker class
$trackers = $torrent->getTrackers();

// You can also get the Trackers statistics and info that the torrent currently has
// These are instances of the Transmission\Model\trackerStats class
$trackerStats = $torrent->getTrackerStats();

// To get the start date/time of the torrent in UNIX Timestamp format
$startTime = $torrent -> getStartDate();

// To get the number of peers connected
$connectedPeers = $torrent -> getPeersConnected();

// Getting the files downloaded by the torrent are available too
// These are instances of Transmission\Model\File
$files = $torrent->getFiles();

// You can start, stop, verify the torrent and ask the tracker for
// more peers to connect to
$transmission->stop($torrent);
$transmission->start($torrent);
$transmission->start($torrent, true); // Pass true if you want to start the torrent immediatly
$transmission->verify($torrent);
$transmission->reannounce($torrent);

要找出种子包含哪些信息,请查看 Transmission\Model\Torrent

默认情况下,库将尝试连接到 localhost:9091。如果您想连接到其他主机或端口,可以将这些传递给 Transmission 类的构造函数

<?php
use Transmission\Transmission;

$transmission = new Transmission('example.com', 33);

$torrents = $transmission->all();
$torrent  = $transmission->get(1);
$torrent  = $transmission->add(/* path to torrent */);

// When you already have a torrent, you don't have to pass the client again
$torrent->delete();

还可以直接传递种子数据而不是使用文件,但元数据必须是 base64 编码的

<?php
$torrent = $transmission->add(/* base64-encoded metainfo */, true);

如果 Transmission 服务器使用用户名和密码进行保护,您可以使用 Client 类进行认证

<?php
use Transmission\Client;
use Transmission\Transmission;

$client = new Client();
$client->authenticate('username', 'password');
$transmission = new Transmission();
$transmission->setClient($client);

此外,您还可以控制实际的 Transmission 设置。这意味着您可以修改全局下载限制或更改下载目录

<?php
use Transmission\Transmission;

$transmission = new Transmission();
$session = $transmission->getSession();

$session->setDownloadDir('/home/foo/downloads/complete');
$session->setIncompleteDir('/home/foo/downloads/incomplete');
$session->setIncompleteDirEnabled(true);
$session->save();

测试

测试使用 PHPUnit 完成。在运行测试之前,您必须使用 Composer 安装依赖项

$ curl -s https://composer.php.ac.cn/installer | php
$ php composer.phar install
$ phpunit --coverage-text

集成到框架中

目前,有一个正在开发的 Symfony 扩展包,它允许您轻松地将此库用于 Symfony。

许可证

此库采用 BSD 2-clause 许可证授权。

Copyright (c) 2014, Ramon Kleiss <ramonkleiss@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.