public function testBatch() { $expected = 'api_key=' . $this->key . '&api_action=batch&api_requestArray=[{"api_action":"linode.create","DatacenterID":"1","PlanID":"1","PaymentTerm":"1"},{"api_action":"linode.create","DatacenterID":"2","PlanID":"1","PaymentTerm":"1"},{"api_action":"linode.create","DatacenterID":"3","PlanID":"1","PaymentTerm":"1"}]'; $batch = new Batch($this->key); $api = new LinodeApi($batch); for ($i = 1; $i <= 3; ++$i) { $api->create($i, 1, PaymentTerm::ONE_MONTH); } $result = $batch->execute(true); self::assertEquals($expected, $result); self::assertFalse($batch->execute(true)); }
/** * Performs specified call to Linode API. * * @param string $action * @param array $parameters * * @return array|bool * * @throws LinodeException * @throws \Exception */ protected function call($action, array $parameters = []) { if ($this->batch) { $query = ['api_action' => $action]; foreach ($parameters as $key => $value) { if ($value !== null) { $query[urlencode($key)] = urlencode($value); } } $this->batch->addRequest($query); return true; } $curl = curl_init(); if (!$curl) { return false; } $query = "api_key={$this->key}&api_action={$action}"; foreach ($parameters as $key => $value) { if ($value !== null) { $query .= sprintf('&%s=%s', urlencode($key), urlencode($value)); } } if ($this->debug) { return $query; } curl_setopt($curl, CURLOPT_URL, 'https://api.linode.com/'); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $query); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($curl); curl_close($curl); $json = json_decode($result, true); if (!$json) { throw new \RuntimeException('Empty response'); } $error = reset($json['ERRORARRAY']); if ($error) { throw new LinodeException($error['ERRORMESSAGE'], $error['ERRORCODE']); } return $json['DATA']; }