PHP code example of daijie / aria2

1. Go to this page and download the library: Download daijie/aria2 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/ */

    

daijie / aria2 example snippets


Aria2 {
    __construct ( string $server [, string $token ] )
    __destruct ( void )
    __call(string $name, array $arg)
    public Object batch( [Callable $func ] )
    public bool inBatch( void )
    public array commit( void )
    protected string req ( array $data )
}

$aria2 = new Aria2('http://127.0.0.1:6800/jsonrpc'); 
// http://127.0.0.1:6800/jsonrpc is the default value, 
// equals to $aria2 = new Aria2
$aria2->getGlobalStat();
$aria2->tellActive();
$aria2->tellWaiting(0,1000);
$aria2->tellStopped(0,1000);
$aria2->addUri(
	['https://www.google.com.hk/images/srpr/logo3w.png'],
	['dir'=>'/tmp']
);
$aria2->tellStatus('1');
$aria2->removeDownloadResult('1');
//and more ...


link = file_get_contents('file.meta4');
$aria2 = new Aria2('http://localhost:6800/jsonrpc');
$c = $aria2->addMetalink($metalink);
#It means the method is aria2.addMetalink
print_r($c); 

$aria2 = new Aria2('http://127.0.0.1:6800/jsonrpc');
$aria2->batch()
      ->getGlobalStat()
      ->tellActive()
      ->tellWaiting(0,1000)
      ->tellStopped(0,1000)
      ->addUri(
			['https://www.google.com.hk/images/srpr/logo3w.png'],
			['dir'=>'/tmp']
		)
		->commit();

$aria2 = new Aria2('http://aria2:6800/jsonrpc', "token:e6c3778f-6361-4ed0-b126-f2cf8fca06db");
$aria2->batch(function($aria2){
    $aria2->getGlobalStat();
    $aria2->system_listMethods();
});
$status = $aria2->commit();

$aria2 = new Aria2('http://127.0.0.1:6800/jsonrpc');
$aria2->system_listMethods();
$aria2->getGlobalStat();

$aria2->addUri(
	['https://www.google.com.hk/images/srpr/logo3w.png'],
	['dir'=>'/tmp']
);

Array
(
    [id] => 1
    [jsonrpc] => 2.0
    [result] => Array
        (
            [completedLength] => 0
            [connections] => 0
            [dir] => /tmp
            [downloadSpeed] => 0
            [errorCode] => 1
            [files] => Array
                (
                    [0] => Array
                        (
                            [completedLength] => 0
                            [index] => 1
                            [length] => 0
                            [path] => 
                            [selected] => true
                            [uris] => Array
                                (
                                    [0] => Array
                                        (
                                            [status] => used
                                            [uri] => https://www.google.com.hk/images/srpr/logo3w.png
                                        )

                                )

                        )

                )
            [gid] => 2
            [numPieces] => 0
            [pieceLength] => 1048576
            [status] => error
            [totalLength] => 0
            [uploadLength] => 0
            [uploadSpeed] => 0
        )

)

Array
(
    [id] => 1
    [jsonrpc] => 2.0
    [result] => Array
        (
            [bitfield] => e0000000
            [completedLength] => 3932160
            [connections] => 1
            [dir] => /data/files/lixian
            [downloadSpeed] => 75972
            [files] => Array
                (
                    [0] => Array
                        (
                            [completedLength] => 3145728
                            [index] => 1
                            [length] => 31550548
                            [path] => /data/files/lixian/茶经.陆羽.扫描版.pdf
                            [selected] => true
                            [uris] => Array
                                (
                                    [0] => Array
                                        (
                                            [status] => used
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                    [1] => Array
                                        (
                                            [status] => waiting
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                    [2] => Array
                                        (
                                            [status] => waiting
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                    [3] => Array
                                        (
                                            [status] => waiting
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                    [4] => Array
                                        (
                                            [status] => waiting
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                )

                        )

                )
            [gid] => 3
            [numPieces] => 31
            [pieceLength] => 1048576
            [status] => active
            [totalLength] => 31550548
            [uploadLength] => 0
            [uploadSpeed] => 0
        )

)

Array
(
    [id] => 1
    [jsonrpc] => 2.0
    [result] => Array
        (
            [bitfield] => fffffffe
            [completedLength] => 31550548
            [connections] => 0
            [dir] => /data/files/lixian
            [downloadSpeed] => 0
            [errorCode] => 0
            [files] => Array
                (
                    [0] => Array
                        (
                            [completedLength] => 31550548
                            [index] => 1
                            [length] => 31550548
                            [path] => /data/files/lixian/茶经.陆羽.扫描版.pdf
                            [selected] => true
                            [uris] => Array
                                (
                                    [0] => Array
                                        (
                                            [status] => used
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                    [1] => Array
                                        (
                                            [status] => waiting
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                    [2] => Array
                                        (
                                            [status] => waiting
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                    [3] => Array
                                        (
                                            [status] => waiting
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                    [4] => Array
                                        (
                                            [status] => waiting
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                    [5] => Array
                                        (
                                            [status] => waiting
                                            [uri] => http://gdl.lixian.vip.xunlei.com/download?fid=zKHWI/O2IbQ07pi/0hPYP1OLwrBUbOEBAAAAACaqKvQbmfR7K7JcbWGT3XQBlDzs&mid=666&threshold=150&tid=3018BA81C31480902DC937770AC2734F&srcid=4&verno=1&g=26AA2AF41B99F47B2BB25C6D6193DD7401943CEC&scn=c7&i=0D2B59F64D6CCBB5A1507A03C3B685BC&t=4&ui=222151634&ti=106821253185&s=31550548&m=0&n=013A830CE1AD5D2EC2DCE21471C9A8C3E8D1D7CA2F64660000&ff=0&co=33BB9833AB0EE7AAEA94105B64C8013F&cm=1
                                        )

                                )

                        )

                )
            [gid] => 3
            [numPieces] => 31
            [pieceLength] => 1048576
            [status] => complete
            [totalLength] => 31550548
            [uploadLength] => 0
            [uploadSpeed] => 0
        )

)

$aria2 = new Aria2('http://aria2:6800/jsonrpc');
$aria2->addUri(
    "token:e6c3778f-6361-4ed0-b126-f2cf8fca06db",
    ['https://www.docker.com/sites/default/files/moby.svg']
);
$aria2->getGlobalStat("token:e6c3778f-6361-4ed0-b126-f2cf8fca06db");

$aria2 = new Aria2('http://aria2:6800/jsonrpc', "token:e6c3778f-6361-4ed0-b126-f2cf8fca06db");
$aria2->addUri(
    ['https://www.docker.com/sites/default/files/moby.svg']
);
$status = $aria2->getGlobalStat();
shell
git clone https://github.com/shiny/php-aria2/
cd php-aria2/playground
docker-compose up
shell
docker-compose exec php composer config repo.packagist composer https://packagist.phpcomposer.com
docker-compose exec php composer 
shell
├── aria2.conf # Aria2 conf file
├── data # Store downloaded file
├── docker-compose.yml
├── nginx.conf # nginx conf
└── www # Web dir
    ├── composer.json
    ├── composer.lock
    ├── index.php
    └── vendor
        ├── autoload.php
        ├── composer
        │   ├── ClassLoader.php
        │   ├── LICENSE
        │   ├── autoload_classmap.php
        │   ├── autoload_namespaces.php
        │   ├── autoload_psr4.php
        │   ├── autoload_real.php
        │   ├── autoload_static.php
        │   └── installed.json
        └── daijie
            └── aria2
                ├── Aria2.php
                ├── LICENSE.txt
                ├── README.md
                └── composer.json