1
0
mirror of https://github.com/ttys3/php-bencode.git synced 2020-06-03 17:07:06 +00:00
Go to file
2020-01-06 00:43:08 +11:00
src fix src/bdict.cc: dict keys should be sorted before encode 2020-01-05 20:38:32 +08:00
tests fix src/bdict.cc: dict keys should be sorted before encode 2020-01-05 20:38:32 +08:00
.clang-format .clang-format; format all codes 2019-09-28 22:56:36 +10:00
.gitignore update .gitignore, remove ide specific ones, add configure.ac 2019-10-01 13:23:35 +10:00
.gitlab-ci.yml add php 7.4 to ci 2019-12-02 14:49:23 +11:00
.travis.yml add php 7.4 to ci 2019-12-02 14:49:23 +11:00
.valgrindrc suppress zend_string_equal_val uninitialised value(s) valgrind warning 2019-09-29 01:48:03 +10:00
config.m4 move source files to src/ 2019-10-01 13:21:22 +10:00
LICENSE upgrade license 2016-09-25 15:37:26 +10:00
php_bencode_doc.php phpdoc updated 2016-05-26 14:49:29 +10:00
php_bencode.h bump version to 1.2.5 2020-01-06 00:43:08 +11:00
README.md update readme, drop todos, add gitlab ci badge 2019-10-01 16:02:46 +10:00

Code Status

Travis CI Travis Status GitLab CI GitLab CI Status

Introduction

The php-bencode is a PHP extension which can boost the process of encoding and decoding of Bencode.

php-bencode now supports only PHP 7 with no need of any external libraries. For PHP 5 support, please check the php-5 branch, which needs PHP-CPP. (PHP 5 may be supported in the future, but it would take me a little long to get familiar with the old Zend APIs. So any PRs are welcomed.)

Installation

Step 1 Install dependencies

# Debian, Ubuntu (from launchpad)
apt-get install php-dev
# Redhat, CentOS, Fedora
yum install php-devel

Step 2 Build and install

git clone https://git.tsundere.moe/Frederick888/php-bencode.git
cd php-bencode
phpize
./configure --enable-bencode
make
make install

Step 3 Enable php-bencode

Add this to your php.ini:

extension=bencode.so
; optional: register classes in "bencode" namespace
bencode.namespace=1

Basic Usage

Example 1 Parsing a string directly

php > $bnode = bitem::parse("d4:key1l5:hello5:worlde4:key2i99ee");
php > print_r($bnode->to_meta_array());
Array
(
    [_type] => bdict
    [_length] => 34
    [_data] => Array
        (
            [key1] => Array
                (
                    [_type] => blist
                    [_length] => 16
                    [_data] => Array
                        (
                            [0] => Array
                                (
                                    [_type] => bstr
                                    [_length] => 7
                                    [_data] => hello
                                )

                            [1] => Array
                                (
                                    [_type] => bstr
                                    [_length] => 7
                                    [_data] => world
                                )

                        )

                )

            [key2] => Array
                (
                    [_type] => bint
                    [_length] => 4
                    [_data] => 99
                )

        )

)
php > $bnode->set('key2', new bint(100));
php > echo $bnode;
d4:key1l5:hello5:worlde4:key2i100ee

Example 2 Loading from/saving to a file

php > $bnode = bitem::load('/path/sample.dat');
php > var_dump($bnode->save('/path/sample_copy.dat'));
bool(true)
php > var_dump(md5_file('/path/sample.dat') === md5_file('/path/sample_copy.dat'));
bool(true)

Example 3 Set/delete a path

it may be a better choice to use set_path/get_path/del_path instead, check phpdoc

php > $bnode = new bdict();
php > $bnode->set('key1', new blist());
php > $bnode->get('key1')->add(new bstr('hello'));
php > $bnode->get('key1')->add(new bstr('world'));
php > print_r($bnode->to_array());
Array
(
    [key1] => Array
        (
            [0] => hello
            [1] => world
        )

)
php > var_dump($bnode->del('key2'));        // inexistent key
bool(false)
php > var_dump($bnode->get('key1')->del(1));
bool(true)
php > print_r($bnode->to_array());
Array
(
    [key1] => Array
        (
            [0] => hello
        )

)