Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1.  public function request($url, $params = [], $post = true)
  2.     {
  3.         $this->reset();
  4.  
  5.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  6.         curl_setopt($this->ch, CURLOPT_HEADER, false);
  7.  
  8.         $apiUrl = trim(parent::$instance->params->url, '/') . '/' . $url;
  9.  
  10.         $headers = [
  11.             //'Content-Type: application/json'
  12.         ];
  13.  
  14.         if (parent::$instance->params->token) {
  15.             $headers[] = 'Token: ' . parent::$instance->params->token;
  16.         } else {
  17.             $headers[] = 'Authorization: Bearer ' . parent::$instance->params->token;
  18.         }
  19.  
  20.         curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
  21.         curl_setopt($this->ch, CURLOPT_POST, $post);
  22.         curl_setopt($this->ch, CURLOPT_URL, $apiUrl);
  23.  
  24.         if ($post) {
  25.             curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($params));
  26.         } else {
  27.             curl_setopt($this->ch, CURLOPT_URL, $apiUrl . '?' . http_build_query($params));
  28.         }
  29.  
  30.         $response = $a = curl_exec($this->ch);
  31.  
  32.         if (!$response) {
  33.             throw new Exception('Invalid request');
  34.         }
  35.  
  36.         // converts string to assoc array
  37.         $response = json_decode($response, true);
  38.  
  39.         if (!$response) {
  40.             throw new Exception('Invalid request');
  41.         }
  42.  
  43.         if (curl_getinfo($this->ch, CURLINFO_HTTP_CODE) != 200) {
  44.             throw new Exception($this->extractError($response));
  45.         }
  46.  
  47.         return $response;
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement