ch = curl_init($server); $this->token = $token; curl_setopt_array($this->ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false, ]); } /** * Allows starting a batch call. * * @return static */ public function batch(): static { $this->batch = true; return $this; } /** * Returns whether it is in a batch call or not. * * @return bool */ public function inBatch(): bool { return $this->batch; } /** * Sends a batch request. * * @return mixed */ public function commit(): mixed { $data = json_encode($this->batch_list); $this->batch = false; $this->batch_list = []; $result = $this->send($data); return json_decode($result); } /** * Sends a request to aria2c via jsonrpc. * * @param string $data * @return string|bool */ protected function send(string $data): string|bool { curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data); return curl_exec($this->ch); } /** * Magic to simplify work :). * * PHP methods are automatically converted into aria2 methods. * * aria2.addUri => Aria2::addUri * aria2.addTorrent => Aria2::addTorrent * aria2.getPeers => Aria2::getPeers * aria2.addMetalink => Aria2::addMetalink * aria2.remove => Aria2::remove * aria2.pause => Aria2::pause * aria2.forcePause => Aria2::forcePause * aria2.pauseAll => Aria2::pauseAll * aria2.forcePauseAll => Aria2::forcePauseAll * aria2.unpause => Aria2::unpause * aria2.unpauseAll => Aria2::unpauseAll * aria2.forceRemove => Aria2::forceRemove * aria2.changePosition => Aria2::changePosition * aria2.tellStatus => Aria2::tellStatus * aria2.getUris => Aria2::getUris * aria2.getFiles => Aria2::getFiles * aria2.getServers => Aria2::getServers * aria2.tellActive => Aria2::tellActive * aria2.tellWaiting => Aria2::tellWaiting * aria2.tellStopped => Aria2::tellStopped * aria2.getOption => Aria2::getOption * aria2.changeUri => Aria2::changeUri * aria2.changeOption => Aria2::changeOption * aria2.getGlobalOption => Aria2::getGlobalOption * aria2.changeGlobalOption => Aria2::changeGlobalOption * aria2.purgeDownloadResult => Aria2::purgeDownloadResult * aria2.removeDownloadResult => Aria2::removeDownloadResult * aria2.getVersion => Aria2::getVersion * aria2.getSessionInfo => Aria2::getSessionInfo * aria2.shutdown => Aria2::shutdown * aria2.forceShutdown => Aria2::forceShutdown * aria2.getGlobalStat => Aria2::getGlobalStat * aria2.saveSession => Aria2::saveSession * system.multicall => Aria2::system_multicall * system.listMethods => Aria2::system_listMethods * system.listNotifications => Aria2::system_listNotifications * * @param array $arg * @return mixed */ public function __call(string $name, array $arg): mixed { if (!is_null($this->token)) { array_unshift($arg, $this->token); } // Allow system methods (the last 3 on the list) if (strpos($name, '_') === false) { // if it doesn't contain underscores, it's an aria2 method command. $name = 'aria2.' . $name; } else { $name = str_replace('_', '.', $name); } $data = [ 'jsonrpc' => '2.0', 'id' => '1', 'method' => $name, 'params' => $arg, ]; // Apply batch call if we are in one if ($this->batch) { $this->batch_list[] = $data; return $this; } $data = json_encode($data); $response = $this->send($data); if ($response === false) { trigger_error(curl_error($this->ch)); } return json_decode($response); } }