BitTorrent library for encoding and decoding torrents in PHP language.

Overview

PHPBitTorrentLib

BitTorrent library for PHP.

This library been tested and works on PHP 7.4+, it originally was just a project to handle the process of encoding/decoding torrents but going forward it will include various other pieces of code.

Bdecoder

Decoding a torrent example.

At present there isn't a validator in this project to validate the contents of a torrent although in the future it will do.

<?php

include('BitTorrent/Encoding/Bdecoder.php');
$bdecoder = new BitTorrent\Encoding\Bdecoder();
$torrent  = file_get_contents("/path/to/torrent.torrent");
$bdecoded = $bdecoder->decode($torrent);
print_r($bdecoded);
Decoding manually.

If you are not sure what type of data your dealing with then it's best to just pass the bencoded data to the decode method and it will determine what the data is, alternatively you can manually ask the decoder to try decoding a specific type as per:

List
$bdecoded = $bdecoder->decodeList('li1ei2ei3ei4ei5ee', 0);
print_r($bdecoded);
Dictionary
$bdecoded = $bdecoder->decodeDict('de', 0);
print_r($bdecoded);
Integer
$bdecoded = $bdecoder->decodeInt('i1024e', 0);
print_r($bdecoded);
String
$bdecoded = $bdecoder->decodeStr('3:lee', 0);
print_r($bdecoded);

As you probably noticed each of the calls above used a second parameter which is the starting offset, in the decode method the offset by default starts at 0 so there isn't any need to provide a starting offset.

If your dealing with a very large torrent in theory you could parse elements of the torrent by using something like strpos and passing the offset to a particular method.

Each of the methods return an array, the first element is the length of the decoded data and second is the value of the decoded data, an example of this can be seen by using a empty dictionary:

de

would yield a result such as:

array(
    [0] => 2, // The size of the encoded data is 2
    [1] = []  // The decoded data is an empty dictionary
)

another example would be:

li1024ei2048ee

would yield a result such as:

array(
   [0] => 14,
   [1] => array(
          [0] => 1024,
          [1] => 2048
       )
)

Bencoder

The bencoder maps PHP data types to specific functionality within the Bencoder class as seen below:

array -> encodeArr

integer -> encodeInt

string -> encodeStr

nulll -> encodeStr

In PHP an array can be a dictionary, list or even mixed as per:

dictionary: ["key" => "val"]

list: [1,2,3,4,5]

mixed: [1, "key" => "val"]

Many bencoding libraries in PHP seem to allow mixed arrays to be encoded which would generate invalid Bencoding, this library only accepts true dictionaries or lists and rejects mixed arrays.

Encoding a dictionary
<?php

include('BitTorrent/Encoding/Bencoder.php');

$bencoder = new BitTorrent\Encoding\Bencoder();
$bencoded = $bencoder->encodeArr([
    'apples' => 1,
    'pears'  => 2
]);
Encoding a list
$bencoded = $bencoder->encodeArr([1,2,3,4,5]);
var_dump($bencoded);
Encoding a string
$bencoded = $bencoder->encodeStr("foo");
var_dump($bencoded);
Encode int
$bencoded = $bencoder->encodeInt(PHP_INT_MAX);
var_dump($bencoded);
Encode null
$bencoded = $bencoder->encode(null);
var_dump($bencoded);

If you are unsure what data your dealing with then simply just pass the data to encode method and it will determine the output for you.

Testing

UnitTests can be located in tests directory.

You might also like...
Rubix ML - A high-level machine learning and deep learning library for the PHP language.

A high-level machine learning and deep learning library for the PHP language. Developer-friendly API is delightful to use 40+ supervised and

This example shows how to use Anychart library with the PHP programming language, Laravel framework and MySQL database.
This example shows how to use Anychart library with the PHP programming language, Laravel framework and MySQL database.

PHP basic template This example shows how to use Anychart library with the PHP programming language, Laravel framework and MySQL database. Running To

Simple custom chat bot developing framework for telegram, qq and more in PHP (the best language)
Simple custom chat bot developing framework for telegram, qq and more in PHP (the best language)

RinoBot RinoBot 是一个为统一聊天机器人扩展开发的框架,编写一份插件用于多种机器人协议。 简体中文 | English 🚧 开发中 🚧 暂不适用于生产环境 特性 插件扩展机制 一份代码运行于多平台多协议机器人 并减小开发难度 插件提供 Yaml 配置 供使用者修改 基于机器人 We

This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 3.25.0 platform

This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 3.25.0 platform. It allows you to hear the sound

A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.
A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

Authoring Tool and Language Workbench for Online Courses

Authoring Tool and Language Workbench for Online Courses

TypeScript and Javascript language support for Atom-IDE
TypeScript and Javascript language support for Atom-IDE

IDE-TypeScript package TypeScript and JavaScript language support for Atom-IDE, powered by the Theia TypeScript Language Server. Early access This pac

Hprose is a cross-language RPC. This project is Hprose 3.0 for PHP
Hprose is a cross-language RPC. This project is Hprose 3.0 for PHP

Hprose 3.0 for PHP Introduction Hprose is a High Performance Remote Object Service Engine. It is a modern, lightweight, cross-language, cross-platform

PHP Expression Language

PHP Expression Language The purpose of this library is to provide a common base for an PHP Expression Language. This is not really a creative library

Releases(v0.1)
Owner
Lee Howarth
Lee Howarth
A bundle to handle encoding and decoding of parameters using OpenSSL and Doctrine lifecycle events.

SpecShaper Encrypt Bundle A bundle to handle encoding and decoding of parameters using OpenSSL and Doctrine lifecycle events. Features include: Master

Mark Ogilvie 48 Nov 4, 2022
PHP Class Encoding featuring popular Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings.

forceutf8 PHP Class Encoding featuring popular \ForceUTF8\Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strin

Sebastián Grignoli 1.6k Dec 22, 2022
YCOM Impersonate. Login as selected YCOM user 🧙‍♂️in frontend.

YCOM Impersonate Login as selected YCOM user in frontend. Features: Backend users with admin rights or YCOM[] rights, can be automatically logged in v

Friends Of REDAXO 17 Sep 12, 2022
PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP language

php-text-analysis PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP l

null 464 Dec 28, 2022
A repository for showcasing my knowledge of the PHP programming language, and continuing to learn the language.

Learning PHP (programming language) I know very little about PHP. This document will list all my knowledge of the PHP programming language. Basic synt

Sean P. Myrick V19.1.7.2 2 Oct 29, 2022
A high-level machine learning and deep learning library for the PHP language.

Rubix ML A high-level machine learning and deep learning library for the PHP language. Developer-friendly API is delightful to use 40+ supervised and

Rubix 1.7k Jan 1, 2023