1. Go to this page and download the library: Download unkybadtouch/qbbittorrent library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
unkybadtouch / qbbittorrent example snippets
use Blackout\Qbittorrent\Client;
$qb = new Client('http://localhost:8080', 'admin', 'adminadmin');
foreach ($qb->getTorrents(['filter' => 'downloading']) as $torrent) {
echo "{$torrent->name}: {$torrent->progress_percent}%\n";
}
use Blackout\Qbittorrent\Client;
$qb = new Client($baseUri, $username, $password);
$torrent = $qb->getTorrents(['hashes' => $hash])[0];
foreach ($torrent->files as $file) { ... } // fires getTorrentFiles() here
foreach ($torrent->files as $file) { ... } // cached, no second request
use Blackout\Qbittorrent\Client;
use Blackout\Qbittorrent\Enum\TorrentState;
$qb = new Client('http://localhost:8080', 'admin', 'adminadmin');
$qb->addTorrentUrls($magnetUri, [
'category' => 'linux',
'paused' => 'false',
// never pass 'savepath' here (PROJECT.md gotcha — qBittorrent mishandles it
// on /torrents/add); set the save path after the add instead:
// $torrent->setSavePath('/data/downloads/linux')
]);
// addTorrentUrls()/addTorrentFile() don't return the new Torrent — the API
// doesn't echo it back — so fetch it once you know the hash (magnet URIs
// carry it in the `xt=urn:btih:...` param; for .torrent files, hash it
// yourself or grep the newest entry in the category)
$hash = strtolower(preg_match('/btih:([a-f0-9]{40})/i', $magnetUri, $m) ? $m[1] : '');
do {
$torrent = $qb->getTorrents(['hashes' => $hash])[0] ?? null;
usleep(500_000);
} while ($torrent === null || $torrent->state === TorrentState::META_DL);
while (!$torrent->is_complete) {
sleep(2);
$torrent = $qb->getTorrents(['hashes' => $hash])[0];
}
echo "{$torrent->name} finished ({$torrent->size_human})\n";
foreach ($torrent->files as $file) { // fires getTorrentFiles($hash) here, once
echo " {$file->basename} ({$file->size_human})\n";
}
$torrent = $qb->getTorrents(['hashes' => $hash])[0];
// Nothing below has fired a request yet — files/trackers/peers/pieces/webseeds
// are declared as hooked `get` properties (Torrent.php), not hydrated fields.
foreach ($torrent->files as $file) { // -> Client::getTorrentFiles($hash)
printf(" [%d] %-40s %6.2f%% prio=%s\n",
$file->index, $file->basename, $file->progress_percent, $file->priority->label());
}
foreach ($torrent->trackers as $tracker) { // -> Client::getTorrentTrackers($hash)
printf(" %-50s %s (%s)\n", $tracker->url, $tracker->status->label(), $tracker->msg);
}
foreach ($torrent->peers as $peer) { // -> Client::getTorrentPeers($hash)
printf(" %-15s:%-5d %-20s %5.1f%%\n", $peer->ip, $peer->port, $peer->client, $peer->progress * 100);
}
$piecesHave = count(array_filter($torrent->pieces, fn ($p) => $p->is_downloaded)); // -> Client::getTorrentPiecesStates($hash)
echo "{$piecesHave}/{$torrent->pieces_num} pieces\n";
foreach ($torrent->webseeds as $seed) { // -> Client::getTorrentWebSeeds($hash)
echo " {$seed->url}\n";
}
// Second access to any of these is free — cached in $torrent->_relations
$torrent->files; // no request
// Mutating through a relation invalidates its own cache so the next read refetches:
$torrent->addTrackers('udp://new-tracker.example.com:80');
$torrent->trackers; // -> Client::getTorrentTrackers($hash) again,
use Blackout\Qbittorrent\DTO\Rss\Feed;
function walkRssTree(array $node, string $indent = ''): void
{
foreach ($node as $name => $item) {
if ($item instanceof Feed) {
// hasError/isLoading/title etc. are only initialized when fetched
// with withData=true — accessing them otherwise throws (uninitialized
// typed property), so guard with isInitialized-style try/catch or,
// as here, only read them when you know withData was passed
$status = $item->hasError ? 'ERROR' : 'ok';
echo "{$indent}{$name} [{$item->url}] {$status}\n";
} else {
echo "{$indent}{$name}/\n";
walkRssTree($item, $indent . ' '); // recurse into the folder
}
}
}
walkRssTree($qb->getRssFeeds(['withData' => true]));
// Direct access once you know the path:
$feed = $qb->getRssFeeds()['tech']['linux-distros'];
$feed->setRefreshInterval(1800);
use Blackout\Qbittorrent\Enum\SearchStatus;
$id = $qb->startSearch('debian netinst iso', plugins: 'all', category: 'all');
do {
sleep(1);
$status = $qb->getSearchStatus($id)[0]; // Search\Status
} while ($status->status === SearchStatus::RUNNING);
echo "{$status->total} results\n";
foreach ($status->results(limit: 25) as $result) { // Search\Result[], via Status::results()
printf("%-60s %5d seeds %s\n", $result->fileName, $result->nbSeeders, $result->engineName);
if ($result->nbSeeders > 10 && str_contains($result->fileName, 'netinst')) {
$result->download(); // -> Client::downloadSearchTorrent()
}
}
$status->delete(); // after this, don't call getSearchStatus($id) again — the id 404s
$rule = $qb->getRssRules()['tv-releases'];
var_dump($rule->torrentParams);
// ['category' => 'tv', 'save_path' => '/data/tv', 'content_layout' => 'Original', ...]
$rule->assignedCategory = 'anime'; // also sets $rule->torrentParams['category'] = 'anime'
$rule->torrentParams['save_path'] = '/data/anime'; // or write the nested array directly — same effect
$rule->save(); // POSTs jsonSerialize() (minus the synthesized `name`) to setRule
$qb->createCategory('archived', savePath: '/data/archive');
foreach ($qb->getTorrentsByCategory('linux') as $torrent) {
if ($torrent->is_complete && $torrent->ratio >= 2.0) {
$torrent->setCategory('archived'); // Client::setTorrentCategory + local field update
$torrent->setSavePath('/data/archive');
}
}
$categories = $qb->getCategories(); // string-keyed array of Category
echo $categories['archived']->savePath; // "/data/archive"
$categories['archived']->edit(savePath: '/data/archive/linux'); // moves + updates local field
foreach ($torrent->files as $file) {
if ($file->priority->isDoNotDownload()) continue;
echo "{$file->basename}: {$file->progress_percent}%\n";
}
$bytes = $qb->downloadFile($hash, $torrent->files[2]); // or pass the index directly
foreach ($torrent->trackers as $tracker) {
if (!$tracker->status->isHealthy()) {
echo "{$tracker->url}: {$tracker->msg}\n";
}
}
foreach ($torrent->peers as $peer) {
echo "{$peer->ip}:{$peer->port} ({$peer->client}) {$peer->progress}\n";
}