-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Aria2.php
85 lines (76 loc) · 1.91 KB
/
Aria2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
class Aria2
{
protected $ch;
protected $token;
protected $batch = false;
protected $batch_cmds = [];
function __construct($server='http://127.0.0.1:6800/jsonrpc', $token=null)
{
$this->ch = curl_init($server);
$this->token = $token;
curl_setopt_array($this->ch, [
CURLOPT_POST=>true,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_HEADER=>false
]);
}
function __destruct()
{
curl_close($this->ch);
}
protected function req($data)
{
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
return curl_exec($this->ch);
}
function batch($func=null)
{
$this->batch = true;
if(is_callable($func)) {
$func($this);
}
return $this;
}
function inBatch()
{
return $this->batch;
}
function commit()
{
$this->batch = false;
$cmds = json_encode($this->batch_cmds);
$result = $this->req($cmds);
$this->batch_cmds = [];
return $result;
}
function __call($name, $arg)
{
if(!is_null($this->token)) {
array_unshift($arg, $this->token);
}
//Support system methods
if(strpos($name, '_')===false) {
$name = 'aria2.'.$name;
} else {
$name = str_replace('_', '.', $name);
}
$data = [
'jsonrpc'=>'2.0',
'id'=>'1',
'method'=>$name,
'params'=>$arg
];
//Support batch requests
if($this->batch) {
$this->batch_cmds[] = $data;
return $this;
}
$data = json_encode($data);
$response = $this->req($data);
if($response===false) {
trigger_error(curl_error($this->ch));
}
return json_decode($response, 1);
}
}