Giter VIP home page Giter VIP logo

php-bittorrent's Introduction

PHP BitTorrent

PHP BitTorrent is a set of components that can be used to interact with torrent files (read+write) and encode/decode to/from the BitTorrent format.

Current Build Status

Requirements

PHP BitTorrent requires PHP 7.2 or above.

Installation

PHP BitTorrent can be installed using Composer:

composer require christeredvartsen/php-bittorrent ^2.0

Using the PHP BitTorrent API

Encode PHP variables

<?php
require 'vendor/autoload.php';

$encoder = new BitTorrent\Encoder();

var_dump($encoder->encodeString('Some string')); // string(14) "11:Some string"
var_dump($encoder->encodeInteger(42)); // string(4) "i42e"
var_dump($encoder->encodeList([1, 2, 3]); // string(11) "li1ei2ei3ee"
var_dump($encoder->encodeDictionary(['foo' => 'bar', 'bar' => 'foo']); // string(22) "d3:foo3:bar3:bar3:fooe"

There is also a convenience method simply called encode in the BitTorrent\Encoder class that can be used to encode all encodable variables (integers, strings and arrays).

Decode BitTorrent encoded data

<?php
require 'vendor/autoload.php';

$decoder = new BitTorrent\Decoder();

var_dump($decoder->decodeString('11:Some string')); // string(11) "Some string"
var_dump($decoder->decodeInteger('i42e')); // int(42)
var_dump($decoder->decodeList('li1ei2ei3ee'); // array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
var_dump($decoder->decodeDictionary('d3:foo3:bar3:bar3:fooe'); // array(2) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "foo" }

There is also a convenience method called decode that can decode any BitTorrent encoded data.

Decode torrent files

The decoder class also has a method for decoding a torrent file (which is an encoded dictionary):

<?php
require 'vendor/autoload.php';

$decoder = new BitTorrent\Decoder();
$decodedFile = $decoder->decodeFile('/path/to/file.torrent');

Create new torrent files and open existing ones

The BitTorrent\Torrent class represents a torrent file and can be used to create torrent files.

<?php
require 'vendor/autoload.php';

$torrent = BitTorrent\Torrent::createFromPath('/path/to/files', 'http://tracker/announce.php')
    ->withComment('Some comment');

$torrent->save('/save/to/path/file.torrent');

The class can also load a torrent file:

<?php
require 'vendor/autoload.php';

$torrent = BitTorrent\Torrent::createFromTorrentFile('/path/to/file.torrent')
    ->withAnnounce('http://tracker/announce.php') // Override announce in original file
    ->withComment('Some comment'); // Override commend in original file

$torrent->save('/save/to/path/file.torrent'); // Save to a new file

License

Licensed under the MIT License.

See LICENSE file.

php-bittorrent's People

Contributors

christeredvartsen avatar mdrollette avatar haehnchen avatar dstendardi avatar vedmant avatar stormwalkerec avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.