Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $config = json_decode(file_get_contents("./config.json"));
- final class ApiRequest {
- const GET = 'GET';
- const POST = 'POST';
- const PUT = 'PUT';
- }
- class Api {
- private $_user;
- private $_password;
- private $_endpoint;
- private $_token = null;
- public function __construct($endpoint) {
- $this->_endpoint = $endpoint;
- }
- /**
- * @param $url
- * @param array $body
- * @param array $params
- * @param string $type
- * @param null $info
- *
- * @return string
- * @throws Exception
- */
- public function request($url, $body = [], $params = [], $type = ApiRequest::GET, &$info = null, $log = false) {
- $headers = [];
- if ($this->_token == null && !isset($body['auth'])) {
- $this->_token = $this->login([
- 'user' => $this->_user,
- 'password' => $this->_password
- ]);
- }
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $this->_endpoint . $url);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
- curl_setopt($curl, CURLINFO_HEADER_OUT, 1);
- if ($this->_token != null) {
- $headers[] = 'X-Authorization-Token: ' . $this->_token;
- }
- if ($type == ApiRequest::POST) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
- } else {
- if ($type == ApiRequest::GET) {
- if(count($body) > 0) {
- $dataString = json_encode($body);
- foreach ([
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($dataString)
- ] as $header) {
- $headers[] = $header;
- };
- curl_setopt($curl, CURLOPT_POSTFIELDS, $dataString);
- }
- }
- }
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- $response = curl_exec($curl);
- $info = curl_getinfo($curl);
- curl_close($curl);
- if ($log) {
- $this->log($url, $response, $info, $info['request_header']);
- }
- if ($info["http_code"] != 200) {
- return null;
- }
- return json_decode($response);
- }
- private function log($text, $response = [], $info = [], $request = []) {
- echo $text . "\n";
- foreach ([
- 'Response' => $response,
- 'Info' => $info,
- 'Request' => $request
- ] as $name => $param) {
- if (count($param) > 0) {
- echo $name . ":\n";
- var_dump($param);
- echo "\n\n";
- }
- }
- for ($i = 0; $i < 20; $i++) {
- echo "-";
- }
- echo "\n";
- }
- /**
- * @param $credentials
- *
- * @return string
- * @throws Exception
- */
- private function login($credentials) {
- //$this->log("Logging in...");
- $response = $this->request('/Core/REST/auth', ['auth' => $credentials], [], ApiRequest::GET, $info);
- $this->_token = $response->result->token;
- //$this->log("Logged in", $response, $info);
- return $this->_token;
- }
- /**
- * @param mixed $user
- */
- public function setUser($user) {
- $this->_user = $user;
- }
- /**
- * @param mixed $password
- */
- public function setPassword($password) {
- $this->_password = $password;
- }
- }
- $api = new Api("https://e-fellows-restdemo.starhunter.software");
- $api->setUser($config->username);
- $api->setPassword($config->password);
- /*$api->login((object)[
- 'user' => $config->username,
- 'password' => $config->password
- ]);*/
- $response = $api->request('/Core/REST/users?offset=0&limit=10?sort=creation_date ', [], [], ApiRequest::GET, $info, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement