martial / transmission-api
该软件包已被废弃且不再维护。没有建议的替代软件包。
Transmission RPC API 的 PHP 客户端。
2.0.1
2017-05-23 20:23 UTC
Requires
- guzzlehttp/guzzle: ~6.2
- psr/log: ~1.0.0
Requires (Dev)
- mockery/mockery: ~0.9.4
- phpunit/phpunit: ~5.1.3
Suggests
- monolog/monolog: Inject Monolog to enhance your logging strategy
README
目的
我编写了这个客户端,因为其他 PHP 客户端缺少功能。此实现是完整的,已使用 PHPUnit 进行测试,并尽可能接近原始 RPC 接口。
安装
使用 composer
composer require 'martial/transmission-api:~2.0'
使用方法
实例化
// Load composer autoloader $httpClient = new GuzzleHttp\Client(['base_uri' => 'http://transmission-server:9091/transmission/rpc']); $api = new \Martial\Transmission\API\RpcClient($httpClient, 'rpc-username', 'rpc-password');
展示你的操作
你可能想使用一个记录器
$logger = new \Monolog\Logger('transmission'); $logger->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout')); $api = new \Martial\Transmission\API\RpcClient($httpClient, 'rpc-username', 'rpc-password', $logger);
会话 ID
你必须为所有 API 方法提供会话 ID 作为第一个参数。这个 ID 可以通过调用这些方法并传递无效的会话 ID,然后捕获 \Martial\Transmission\API\CSRFException 来检索。
$sessionId = ''; try { $api->sessionGet($sessionId); } catch (\Martial\Transmission\API\CSRFException $e) { // The session has been reinitialized. Fetch the new session ID with the method getSessionId(). $sessionId = $e->getSessionId(); } catch (\Martial\Transmission\API\TransmissionException $e) { // The API returned an error, retrieve the reason with the method getResult(). die('API error: ' . $e->getResult()); }
方法使用示例
然后,只需阅读 \Martial\Transmission\API\TransmissionAPI 接口的文档。每个方法都有文档说明。
try { $api->torrentAdd($sessionId, [ \Martial\Transmission\API\Argument\Torrent\Add::FILENAME => '/path/to/the/torrent/file.torrent' ]); } catch (\Martial\Transmission\API\DuplicateTorrentException $e) { // This torrent is already in your download queue. } catch (\Martial\Transmission\API\MissingArgumentException $e) { // Some required arguments are missing. } catch (\Martial\Transmission\API\CSRFException $e) { // The session has been reinitialized. Fetch the new session ID with the method getSessionId(). } catch (\Martial\Transmission\API\TransmissionException $e) { // The API returned an error, retrieve the reason with the method getResult(). die('API error: ' . $e->getResult()); }