Advertisement
Guest User

Untitled

a guest
Apr 13th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.46 KB | None | 0 0
  1. <?php
  2.  
  3. $config = json_decode(file_get_contents("./config.json"));
  4.  
  5. final class ApiRequest {
  6.  
  7.     const GET = 'GET';
  8.     const POST = 'POST';
  9.     const PUT = 'PUT';
  10. }
  11.  
  12. class Api {
  13.  
  14.     private $_user;
  15.  
  16.     private $_password;
  17.  
  18.     private $_endpoint;
  19.  
  20.     private $_token = null;
  21.  
  22.     public function __construct($endpoint) {
  23.  
  24.         $this->_endpoint = $endpoint;
  25.     }
  26.  
  27.     /**
  28.      * @param        $url
  29.      * @param array  $body
  30.      * @param array  $params
  31.      * @param string $type
  32.      * @param null   $info
  33.      *
  34.      * @return string
  35.      * @throws Exception
  36.      */
  37.     public function request($url, $body = [], $params = [], $type = ApiRequest::GET, &$info = null, $log = false) {
  38.  
  39.         $headers = [];
  40.  
  41.         if ($this->_token == null && !isset($body['auth'])) {
  42.             $this->_token = $this->login([
  43.                 'user' => $this->_user,
  44.                 'password' => $this->_password
  45.             ]);
  46.         }
  47.  
  48.         $curl = curl_init();
  49.         curl_setopt($curl, CURLOPT_URL, $this->_endpoint . $url);
  50.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  51.         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  52.         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  53.         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
  54.         curl_setopt($curl, CURLINFO_HEADER_OUT, 1);
  55.  
  56.         if ($this->_token != null) {
  57.             $headers[] = 'X-Authorization-Token: ' . $this->_token;
  58.         }
  59.  
  60.         if ($type == ApiRequest::POST) {
  61.  
  62.             curl_setopt($curl, CURLOPT_POST, 1);
  63.             curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
  64.  
  65.         } else {
  66.             if ($type == ApiRequest::GET) {
  67.                 if(count($body) > 0) {
  68.                     $dataString = json_encode($body);
  69.                     foreach ([
  70.                                  'Content-Type: application/json',
  71.                                  'Content-Length: ' . strlen($dataString)
  72.                              ] as $header) {
  73.                         $headers[] = $header;
  74.                     };
  75.                     curl_setopt($curl, CURLOPT_POSTFIELDS, $dataString);
  76.                 }
  77.             }
  78.         }
  79.  
  80.         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  81.  
  82.         $response = curl_exec($curl);
  83.         $info = curl_getinfo($curl);
  84.         curl_close($curl);
  85.  
  86.         if ($log) {
  87.             $this->log($url, $response, $info, $info['request_header']);
  88.         }
  89.  
  90.         if ($info["http_code"] != 200) {
  91.             return null;
  92.         }
  93.  
  94.         return json_decode($response);
  95.     }
  96.  
  97.     private function log($text, $response = [], $info = [], $request = []) {
  98.  
  99.         echo $text . "\n";
  100.         foreach ([
  101.                      'Response' => $response,
  102.                      'Info' => $info,
  103.                      'Request' => $request
  104.                  ] as $name => $param) {
  105.             if (count($param) > 0) {
  106.                 echo $name . ":\n";
  107.                 var_dump($param);
  108.                 echo "\n\n";
  109.             }
  110.         }
  111.         for ($i = 0; $i < 20; $i++) {
  112.             echo "-";
  113.         }
  114.         echo "\n";
  115.     }
  116.  
  117.     /**
  118.      * @param $credentials
  119.      *
  120.      * @return string
  121.      * @throws Exception
  122.      */
  123.     private function login($credentials) {
  124.  
  125.         //$this->log("Logging in...");
  126.         $response = $this->request('/Core/REST/auth', ['auth' => $credentials], [], ApiRequest::GET, $info);
  127.         $this->_token = $response->result->token;
  128.  
  129.         //$this->log("Logged in", $response, $info);
  130.         return $this->_token;
  131.     }
  132.  
  133.     /**
  134.      * @param mixed $user
  135.      */
  136.     public function setUser($user) {
  137.  
  138.         $this->_user = $user;
  139.     }
  140.  
  141.     /**
  142.      * @param mixed $password
  143.      */
  144.     public function setPassword($password) {
  145.  
  146.         $this->_password = $password;
  147.     }
  148. }
  149.  
  150. $api = new Api("https://e-fellows-restdemo.starhunter.software");
  151. $api->setUser($config->username);
  152. $api->setPassword($config->password);
  153. /*$api->login((object)[
  154.     'user' => $config->username,
  155.     'password' => $config->password
  156. ]);*/
  157.  
  158. $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