root/trunk2/example.php

Revision 77, 3.9 KB (checked in by m, 17 months ago)

Renaming to File_Bittorrent2.

  • Property svn:keywords set to Id
Line 
1<?php
2
3// +----------------------------------------------------------------------+
4// | Decode and Encode data in Bittorrent format                          |
5// +----------------------------------------------------------------------+
6// | Copyright (C) 2004-2005 Markus Tacker <m@tacker.org>                 |
7// +----------------------------------------------------------------------+
8// | This library is free software; you can redistribute it and/or        |
9// | modify it under the terms of the GNU Lesser General Public           |
10// | License as published by the Free Software Foundation; either         |
11// | version 2.1 of the License, or (at your option) any later version.   |
12// |                                                                      |
13// | This library is distributed in the hope that it will be useful,      |
14// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
16// | Lesser General Public License for more details.                      |
17// |                                                                      |
18// | You should have received a copy of the GNU Lesser General Public     |
19// | License along with this library; if not, write to the                |
20// | Free Software Foundation, Inc.                                       |
21// | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA               |
22// +----------------------------------------------------------------------+
23
24/**
25* Example usage of File_Bittorrent2
26*
27* @author Markus Tacker <m@tacker.org>
28* @version $Id$
29*/
30
31/**
32* Include class
33*/
34require_once 'File/Bittorrent2/Encode.php';
35require_once 'File/Bittorrent2/Decode.php';
36
37$File_Bittorrent2_Decode = new File_Bittorrent2_Decode;
38$File_Bittorrent2_Encode = new File_Bittorrent2_Encode;
39
40// Encoding vars
41echo "Encoding integers\n";
42$encodedInt = $File_Bittorrent2_Encode->encode(10);
43var_dump($encodedInt);
44var_dump($File_Bittorrent2_Decode->decode($encodedInt));
45
46echo "Encoding strings\n";
47$encodedStr = $File_Bittorrent2_Encode->encode('This is a string.');
48var_dump($encodedStr);
49var_dump($File_Bittorrent2_Decode->decode($encodedStr));
50
51echo "Encoding arrays as lists\n";
52$encodedList = $File_Bittorrent2_Encode->encode(array('Banana', 'Apple', 'Cherry'));
53var_dump($encodedList);
54var_dump($File_Bittorrent2_Decode->decode($encodedList));
55
56echo "Encoding arrays as dictionaries\n";
57$encodedDict = $File_Bittorrent2_Encode->encode(array('fruits' => array('Banana', 'Apple', 'Cherry','subarray' => array(1,2,3)), 'ints' => array(1,2,3), 'count' => 3));
58var_dump($encodedDict);
59print_r($File_Bittorrent2_Decode->decode($encodedDict));
60
61// Decode a file
62print_r($File_Bittorrent2_Decode->decodeFile('install-x86-universal-2005.0.iso.torrent'));
63
64/* Output of decode
65
66Array
67(
68    [count] => 3
69    [fruits] => Array
70        (
71            [0] => Banana
72            [1] => Apple
73            [2] => Cherry
74            [subarray] => Array
75                (
76                    [0] => 1
77                    [1] => 2
78                    [2] => 3
79                )
80
81        )
82
83    [ints] => Array
84        (
85            [0] => 1
86            [1] => 2
87            [2] => 3
88        )
89
90)
91Array
92(
93    [name] => install-x86-universal-2005.0.iso
94    [filename] => install-x86-universal-2005.0.iso.torrent
95    [comment] =>
96    [date] => 1111915968
97    [created_by] =>
98    [files] => Array
99        (
100            [0] => Array
101                (
102                    [filename] => install-x86-universal-2005.0.iso
103                    [size] => 712460288
104                )
105
106        )
107
108    [size] => 712460288
109    [announce] => http://titmouse.gentoo.org:6969/announce
110    [announce_list] => Array
111        (
112        )
113
114    [info_hash] => f94b8d2d38632a6589d8121106059989f290b569
115)
116
117*/
118
119?>
Note: See TracBrowser for help on using the browser.